一看大数妈呀,怎么做,一看取模就笑了。。
大于2009的数的阶乘一模2009就没了。。
直接从0到2009打表。。
做完一搜大神的阶梯报告。。根本不需要到2009.。。
/*
2009=287*7=41*49=41*7*7。所以当n>=41时,n!%2009=0。
n<41时,用公式:(a*b)%c = ((a%c) * (b%c)) % c。
*/。。。感觉智商又被碾压了
我的代码:
#include "iostream"
#include "cmath"
using namespace std;
int s[3000]= {1};
void ini()
{
for (int i = 1;i< 2010;++i)
s[i] = (s[i-1]*i)%2009;
}
int main()
{
ini();
int t;
while (cin >> t)
{
if (t >= 2009)
cout << s[2009] <<endl;
else
cout << s[t] << endl;
}
return 0;
}
大神代码:
#include<iostream>
using namespace std;
int main()
{
long long n, i ,s;
while(cin >> n)
{
if (n == 0)
{
cout << "1" << endl;
continue;
}
if (n >= 41)
{
cout << "0" << endl;
continue;
}
s=n%2009;
for (i=n-1; i>=1; i--)
{
s=((s)*(i%2009))%2009;
}
cout << s << endl;
}
return 0;
}