刚开始没有注意那个Hint : 0! = 1, N! = N*(N-1)!结果WA了!本题N的数据可以到10的9次方,暴力就算了,冷静分析一下吧!
阶乘数的一个特点就是当n的阶乘数的可以整除一个数m时,那么比n大的阶乘数都含有这样的因子;
因此,本题只需要计算出第一个n的阶乘数f(n),满足f(n)mod2009=0;即可。
后面的数只要大于等于n,直接输出“0”即可。
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 #define N 1000 5 long long num[N]; 6 long long lim; 7 void handle() 8 { 9 long long fac=1; 10 long long i; 11 num[0]=1; 12 for(i=1;i<=1000;i++) 13 { 14 if(fac%2009==0) break; 15 fac=fac*i; 16 fac%=2009; 17 num[i]=fac%2009; 18 } 19 lim=i-1; 20 // cout<<lim<<endl; 21 } 22 int main() 23 { 24 handle(); 25 long long n; 26 while(cin>>n) 27 { 28 if(n<=lim) 29 cout<<num[n]<<endl; 30 else 31 cout<<"0"<<endl; 32 33 } 34 return 0; 35 36 }
Problem Description
WhereIsHeroFrom: Zty, what are you doing ?
Zty: I want to calculate N!......
WhereIsHeroFrom: So easy! How big N is ?
Zty: 1 <=N <=1000000000000000000000000000000000000000000000…
WhereIsHeroFrom: Oh! You must be crazy! Are you Fa Shao?
Zty: No. I haven's finished my saying. I just said I want to calculate N! mod 2009
Hint : 0! = 1, N! = N*(N-1)!
Zty: I want to calculate N!......
WhereIsHeroFrom: So easy! How big N is ?
Zty: 1 <=N <=1000000000000000000000000000000000000000000000…
WhereIsHeroFrom: Oh! You must be crazy! Are you Fa Shao?
Zty: No. I haven's finished my saying. I just said I want to calculate N! mod 2009
Hint : 0! = 1, N! = N*(N-1)!
Input
Each line will contain one integer N(0 <= N<=10^9). Process to end of file.
Output
For each case, output N! mod 2009
Sample Input
4
5
Sample Output
24
120