N!Again (同余)

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
题意:

求N!mod 2009

分析:

这是阶乘啊,是乘起来的数,所以2009!以后每个数的阶乘都含有2009这个因数了,取模一定为0,所以实际需要算的只有2009之前的阶乘

code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const double e = exp(1.0);
const double pi = acos(-1.0);
const ll mod = 2009;
ll fac[3000];
void init(){
    fac[0] = fac[1] = 1;
    for(ll i = 2; i <= 3000; i++){
        fac[i] = (fac[i-1] * i) % mod;
    }
}
int main(){
    ll n;
    init();
    while(scanf("%lld",&n) != EOF){
        if(n >= 2009)
            printf("0\n");
        else
            printf("%lld\n",fac[n]);
    }
    return 0;
}
#include<stdio.h> #include <stdlib.h> #include <unistd.h> void clearScreen() { system("cls"); } const int l=21; //比较大小 int max(int a,int b){ if (a>b){ printf("\n一号玩家胜\n"); return 0; } else if(a<b){ printf("\n二号玩家胜\n"); return 0; } else { printf("平局\n"); return 0; } } int main(){ int a,b,c,m,n,j,i; i=m=n=0; c=1; printf("欢迎来到21点游戏\n"); again_0: while(c){ i++; srand((unsigned int)time(NULL)); int random = rand()%10 +1; switch(j){ case 1: goto again_1; case 2: goto again_2; } again_1: printf("\n请一号玩家选择\n1.抽牌\n2.不抽\n3.开牌\n"); scanf("%d",&a); switch(a){ case 1: m+=random; if (m>l){ printf("一号手牌大小为%d\n",m); printf("一号游戏失败"); goto again_3; } else { printf("一号手牌大小为%d\n",m); j=2; sleep(1); clearScreen(); goto again_0; break; } case 2: j=2; goto again_0; break; case 3: if (i>4){ max(m,n); c=0; goto again_0; } else{ printf("当前无法比较大小\n请到第三轮\n"); goto again_1; } break; default: printf("\n输入数字无效\n请重新输入"); goto again_1; break; } again_2: printf("\n请二号玩家选择\n1.抽牌\n2.不抽\n3.开牌\n"); scanf("%d",&b); switch(b){ case 1: n+=random; if (n>l){ printf("二号手牌大小为%d\n",n); printf("二号游戏失败"); goto again_3; } else { printf("二号手牌大小为%d\n",n); j=1; sleep(1); clearScreen(); goto again_0; break; } case 2: j=1; goto again_0; break; case 3: if (i>4){ max(m,n); c=0; goto again_0; } else{ printf("当前无法比较大小\n请到第三轮\n"); } break; default: printf("输入数字无效\n请重新输入"); goto again_2; break; } } again_3: printf("\n继续游戏输1\n结束游戏输0\n"); scanf("%d",&c); if(c=1){ m=n=i=0; goto again_0; } else{ printf("游戏结束"); } } 如何再次基础上可以不输入enter,输入123可以自动执行
10-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值