1. 程式人生 > >【51Nod-1008】N的階乘 mod P

【51Nod-1008】N的階乘 mod P

                                                       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的結果。 Sample Input
10 11
Sample Output
10

//程式碼如下:

#include <cstdio>
#define ll long long
int main()
{
     ll  n,p;
	scanf("%lld%lld",&n,&p);
	 ll ans = 1;
	if(n == 0)
	{
		printf("%lld\n",1%p);
	}
	else
	{
		for (ll  i = 1; i <= n; i++)
	  {
	  	   ans = ans%p*i%p;
	  }
	printf("%lld\n",ans);
	}
	
return 0;
}