51Nod 1008 N的阶乘 mod P

输入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

同余定理:(a+b)%m=(a%m+b%m)%m    a*b%m=(a%m*b%m)%m 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 #include <stdio.h>
 6 using namespace std;
 7 #define LL long long
 8 int main()
 9 {
10     LL n,p;
11     scanf("%lld%lld",&n,&p);
12     if(n==0){
13         printf("%lld\n",1%p);
14     }
15     else{
16         LL sum=1;
17         for(LL i=1;i<=n;++i){
18             sum=sum%p*i%p;
19         }
20         printf("%lld\n",sum);
21     }
22     return 0;
23 }

 

posted @ 2017-07-26 00:35  wydxry  阅读(543)  评论(0编辑  收藏  举报
Live2D