hdu 2674 N!Again

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以后的阶乘都包含有2009这个因子,所以对2009取模都是0,结论,10的9次方纯属吓唬人的!再深入点,其实2009=41x7x7 也就是到40以后都是0了!

代码如下:

#include<stdio.h>
int main()
{
    int n,sum,i,a[41];
    a[0]=a[1]=1;sum=1;
    for(i=2;i<=40;i++)
    {
        sum*=i;
        sum%=2009;
        a[i]=sum;
    }
    while(scanf("%d",&n)!=EOF)
    {
        if(n<=40) printf("%d\n",a[n]);
        else printf("0\n");
    }
    return 0;
}
    

 

 

posted @ 2013-12-20 15:17  段少  阅读(193)  评论(0编辑  收藏  举报