hdu_3123 GCC

来源:互联网 发布:五五开笑笑知乎 编辑:程序博客网 时间:2024/06/15 10:11
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define N 110char a[N];int main(){    int t,n,m;    scanf("%d",&t);    while(t--){        scanf("%s%d",&a,&m);        int l=strlen(a);        int ans;        if(l<7){            int temp=0;            for(int i=0;i<l;i++){                temp=temp*10+a[i]-'0';            }            ans=min(temp,m);        }        else {            ans=m;        }        __int64 cnt=1%m,pre=1;        for(int i=1;i<=ans;i++){            pre=(pre*i)%m;            cnt=(cnt+pre)%m;            if(pre==0)break;        }        printf("%I64d\n",cnt);    }    return 0;}
//
分析下题就知道了。。大于m的数的阶乘对m取余一定为0.
还有就是要用__int64 因为在相乘的时候会超出int范围
原创粉丝点击