hdoj 2647 N!Again

N!Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4016    Accepted Submission(s): 2157


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
 
此题仍然用同余定理:前边文章中已经做出详细解释这里就不解释了
 此题还有一个技巧就是41(包括41)之后的所有数据结果都是0
因为40求出的结果是245==49*5      2009==49*41
下一步对41求阶乘并对2009取模就等于(41%2009*245%2009)%2009==(41*245)%2009==0
所以之后的每一步都等0
AC代码:
#include<stdio.h>
#include<string.h>
int main()
{
	int n,m,j,i,s,t;
	while(scanf("%d",&n)!=EOF)
	{
		if(n>=41)
		printf("0\n");
		else
		{	
		    s=1;
		    for(i=2;i<=n;i++)
		    {
			    s=s%2009*i%2009;
			    s=s%2009;
		    }
		    printf("%d\n",s);
	    }
	}
	return 0;
}

  

posted @ 2015-07-25 17:10  非我非非我  阅读(213)  评论(0编辑  收藏  举报