N!Again
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3803 Accepted Submission(s): 2036
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)!
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
这题最容易想到的是2009以后全是0。。。至少我第一次A的代码就是这个思路。2009以前的打表~~
其次,2009=41*7*7。所以40以后全都是0了。至于40以前的也是打表呗~
附上我的代码:
#include <stdio.h> #define MAX 2009 int ans[MAX] ; int main() { int n ; ans[0] = 1 ; for(int i = 1 ; i < MAX ; ++i ) { ans[i] = (ans[i-1]*i)%MAX ; } while(~scanf("%d",&n)) { if(n>=2009) { puts("0") ; } else { printf("%d\n",ans[n]) ; } } return 0; }
与君共勉