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
题意:
求n的阶乘!!
思路:
如果输入的n大于等于2009,那么最终阶乘取余2009等于0,所以这里可以特判,然后打表就可!
代码:
#include<stdio.h>
#include<math.h>
int a[2100];
int main()
{
long long n,i;
a[0]=a[1]=1;
for(i=2;i<=2009;i++)
a[i]=a[i-1]*i%2009;
while(~scanf("%lld",&n))
{
if(n>=2009)
printf("0\n");
else
printf("%d\n",a[n]);
}
return 0;
}
本文介绍了一种快速计算N的阶乘并对2009取余的算法,通过预先计算并存储小范围内的阶乘值,对于任意输入N(0<=N<=10^9),能够迅速给出N!mod2009的结果,特别针对N大于等于2009的情况进行了优化。
158

被折叠的 条评论
为什么被折叠?



