HDU 2674 N!Again

来源:互联网 发布:apache官网下载tomcat 编辑:程序博客网 时间:2024/05/16 04:56
Calculate N! mod 2009
 
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

5
 
Sample Output
24

120


2009以后的阶乘都包含有2009这个因子,所以对2009取模都是0,
结论,10的9次方纯属吓唬人的!再深入点,其实2009=41x7x7 也就是40以后都是0


#include<stdio.h>int main(){int n,i,s;while(scanf("%d",&n)!=EOF){if(n>40)//虽然没有这个if直接算也是正确,但超时{printf("0\n");continue;}s=1;for(i=2;i<=n;i++){s*=i;s%=2009;}printf("%d\n",s);}return 0;}


0 0
原创粉丝点击