N!Again(同余定理)

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

 我们将  2009  分成三个素数   41*7*7  ,若n大于等于41,那么求阶乘的话必然含有 41,7,14(2*7)来组成2009,那么对  2009 取模显然是0

 

//#pragma GCC optimize(2)
#include 
#include 
#include 
#include 
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const ll inf=1e18;
const int N=1e6+5;
const int mod=2009;

    int n,m,t;
    int i,j,k;
    //int a[N];

int main()
{
    cin.tie(0);istream::sync_with_stdio(false);
    while(cin>>n){
        if(n>=41) cout<<"0\n";
        else{
            int ans=1;
            for(i=2;i<=n;i++) ans=((ans)*(i%mod))%mod;
            cout<

 

你可能感兴趣的:(HDU,#,数论)