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)!
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[N];
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[n]);
return 0;
}菜鸟成长记

11万+

被折叠的 条评论
为什么被折叠?



