您的位置:首页 > 其它

51nod 1008 N的阶乘 mod P

2015-10-16 18:40 197 查看
输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %)

例如:n = 10, P = 11,10! = 3628800
3628800 % 11 = 10

Input
两个数N,P,中间用空格隔开。(N < 10000, P < 10^9)


Output
输出N! mod P的结果。


Input示例
10 11


Output示例
10


变乘边取余,注意用long long来存,防止爆int

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
int main()
{
ll n,mod,ans;
ans=1;
cin>>n>>mod;
if(n>=mod) {
cout<<"0"<<endl;
return 0;
}
for(ll i=1;i<=n;i++) {
ans=ans%mod*i%mod;
}
cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: