例:
输入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的结果。
Sample Input
10 11
Sample Output
10
#include<cstdio>
using namespace std;
typedef long long ll;
ll factorial_mod(ll n,ll mod){
ll res = 1;
for(ll i = 1;i<=n;i++){
res *= i%mod;
res = res % mod;
}
return res;
}
int main(){
ll n,mod;
scanf("%lld%lld",&n,&mod);
ll ans = factorial_mod(n,mod);
printf("%lld\n",ans);
return 0;
}