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;
    }
    

      

  • 相关阅读:
    MSSQL跨服务器插入
    TCP/IP详解 笔记一
    android学习十三 首选项
    android 学习十四 探索安全性和权限
    android学习十二 配置变化
    android学习十 ActionBar
    android学习十一 高级调试分析功能
    android学习九 对话框碎片
    android学习八 多用途碎片
    android学习七 菜单
  • 原文地址:https://www.cnblogs.com/tonghao/p/4676233.html
Copyright © 2011-2022 走看看