hdu--2674
2015-12-01 18:47
267 查看
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
Sample Output
解体思路:(1*2*3*4..............*n)%2009=((.....((1%2009)*2)%2009.....)*n)%2009;又2009=7*7*41,所以比41大的数结果都为0;问题缩小到了41。接下来处理问题就变得简单多了。
代码如下:
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
解体思路:(1*2*3*4..............*n)%2009=((.....((1%2009)*2)%2009.....)*n)%2009;又2009=7*7*41,所以比41大的数结果都为0;问题缩小到了41。接下来处理问题就变得简单多了。
代码如下:
#include<stdio.h> #include<string.h> int main(){ long long n,m; while(scanf("%lld",&n)!=EOF){ if(n>=41){ printf("0\n"); continue; } else if(n==0){ printf("1\n"); } else { m=1; for(int i=1;i<=n;i++){ m=(m*i)%2009; } printf("%d\n",m%2009); } } return 0; }
相关文章推荐
- C++之UML关系说明图
- Linux目录下/dev/shm的理解和使用
- ISAPI_Rewrite3.x中文乱码解决办法
- 学习笔记File类
- Python 序列化
- BZOJ1911(斜率优化DP)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Opencv中gemm的使用
- python 3 ini配置文件操作
- java实现文件上传到七牛服务器
- java的socket网络编程
- Spark的Transform与Action操作(important)
- WIFI之系统部署环境
- 继承第一课 *重要
- ASP.NET母版与内容页相对路径的问题
- EAS8.0解决方案需要构建的jar目录
- thrift资源
- linux常用快捷键随笔
- poj1125 Dijkstra
- 测试利用Binder进行双向通信