平方模型为零,阶乘模型问题
modelo of square is zero, factorial modelo problem
我正在做CS作业,问题是得到n!,其中n <=10^6,为了适合它,我需要n! % m,
我编写了以下代码:
#include <bits/stdc++.h>
using namespace std;
int main ()
{
long long n, m = 7+10e9, fact = 1;//value of m is given
cin>>n;
n++;
while (--n)
{
fact = ((fact%m)*(n%m))%m;
cout<<fact<<endl;//added this of debugging
if (fact == 0) break;//and this also
}
cout <<fact;
return 0;
}
代码对于小数字(即 <10^5(很好,但是当数字变大时,事实输出为 0,在每一步中打印事实后,我意识到当由于某种原因 fact(x( == 事实 (x-1((x 是步骤(时,新事实为零
例如,当我输入 10^6 时,事实连续两次8478216162,然后得到零。
提前感谢任何帮助
你的代码中有两个错误。
m = 7+10^9
.这里的^
意味着bitwise xor
操作员,而不是权力。你应该把它写m = 7 + 10e9
或m = 1000000007
if (fact = 0)
.当你给出单equal
时,它不是检查。而不是将事实值分配给 0。所以写双等于。if (fact == 0)
你的完整代码应该是这样的 -
#include <bits/stdc++.h>
using namespace std;
int main () {
long long n, m = 7+10e9, fact = 1;//value of m is given
cin>>n;
n++;
while (--n) {
fact = ((fact%m)*(n%m))%m;
//cout<<fact<<endl;//added this of debugging
if (fact == 0)
break;//and this also
}
cout <<fact;
return 0;
}
相关文章:
- 在 C++ 中的数组上使用阶乘函数
- 阶乘问题在 c++ 中给出错误的输出
- 大数的阶乘给出错误的输出
- 什么模板用法在阶乘中更好
- 为什么我在C++阶乘函数中出现编译错误?
- 计算阶乘的 C++17 倍表达式中的错误
- 为什么 Lisp 中 1000 阶乘的计算如此之快(并显示正确的结果)?
- 在C++中执行 N 阶乘编译时间的 3 种不同/相同方法
- 平方模型为零,阶乘模型问题
- 阶乘元规划
- 阶乘-c++ 我想以不同的方式打印
- 阶乘函数只返回C++中输入的答案
- 阶乘使用循环递增给出垃圾值
- 大 n 表示阶乘
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- 计算最大数字的阶乘
- 这个阶乘程序内部发生了什么?
- 返回阶乘C++的递归函数
- C++ 中是否有任何内置阶乘函数?
- 高效计算阶乘