HDU 2674 N!Again(规律)
2015-08-25 19:11
477 查看
N!Again
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)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
/************************************************************************/
题意:求N! mod 2009
orz
也是醉了,刚开始还傻呆呆地找规律,利用同余一直求到了41!,发现之后的结果全是0,这时才反应过来,N! mod 2009 ,当N!能够被2009整除时,取模的结果均为0,那剩下的就是判断一下什么时候能够被2009整除
对2009分解质因数,可得2009=7*7*41,所以我们使N!有约数41就可以被2009整除了,所以才会从41!开始 mod 2009的结果均为0
因此我们只需先算好前40项mod 2009的结果就可以了,N>40的结果都是0
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 41; const int inf = 2147483647; const int mod = 2009; int s ; int main() { int n,i; s[0]=1; for(i=1;i<=40;i++) s[i]=(s[i-1]*i)%mod; while(~scanf("%d",&n)) if(n>40) puts("0"); else printf("%d\n",s ); return 0; }菜鸟成长记
相关文章推荐
- AIZU 2560 [想法题]
- hdu 5327 Olympiad 2015 Multi-University Training Contest 4
- weblogic.management.ManagementException: The domain edit lock is owned by another session in ...
- mail发邮件包警告
- hdu 5328 Problem Killer 2015 Multi-University Training Contest 4
- HDU 1867 A + B for you again KMP解决问题的方法
- 找不到rails console命令
- POJ-1363 Rails-堆栈入门
- 找不到rails console命令
- 绕过google的手机验证注册gmail邮箱
- Train Problem I
- Repairing or Restoring an Inconsistent OCR in RAC
- Train Problem II
- Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name
- HDU 2473 ( Junk-Mail Filter ) (并查集删除操作)
- 用Java开源项目JOONE实现人工智能编程
- Liberty OpenID TAI configuration
- hdu 5335 Walk Out 2015 Multi-University Training Contest 4
- Jenkins 邮件配置 (使用 Jenkins Email Extension Plugin)
- error LNK2019: 无法解析的外部符号 _WinMain@16(VS2013)