• 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
    ");
    		else
    		{	
    		    s=1;
    		    for(i=2;i<=n;i++)
    		    {
    			    s=s%2009*i%2009;
    			    s=s%2009;
    		    }
    		    printf("%d
    ",s);
    	    }
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    contest hunter5105 Cookies
    bzoj2599: [IOI2011]Race
    poj1741 Tree
    bzoj2527: [Poi2011]Meteors
    bzoj3673: 可持久化并查集 by zky&&3674: 可持久化并查集加强版
    bzoj2741: 【FOTILE模拟赛】L
    bzoj3110: [Zjoi2013]K大数查询
    bzoj1901: Zju2112 Dynamic Rankings
    bzoj2821: 作诗(Poetize)
    poj1417 True Liars
  • 原文地址:https://www.cnblogs.com/tonghao/p/4676233.html
Copyright © 2020-2023  润新知