zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    Vue框架构造
    JavaScript-改变this指向
    前端发展史
    python篇第10天【For 循环语句】
    python篇第10天【While 循环语句】
    python篇第8天【运算符】
    python篇第6天【数据类型】
    python篇第5天【变量】
    Python篇函数总结【输出函数】
    python篇第3天【编码规范】
  • 原文地址:https://www.cnblogs.com/tonghao/p/4676233.html
Copyright © 2011-2022 走看看