Wilson's theorem
Elementary number theory prime when and only if p for Prime: (P-1)! ≡-1 (mod p), but due to factorial
Hdu5391 used this theorem of number theory.
Zball in Tina townTime
limit:3000/1500 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 276 Accepted Submission (s): 168
Problem Descriptiontina Town was a friendly place. People there care on each of the other.
Tina has a ball called Zball. Zball is magic. It grows larger every day. On the first day, it becomes 1 Time as large as its original size. On the second day,it'll become 2 Times as large as the size on the first day. On the n-th day,it'll become n Times as large as the size on the (n-1)-th Day. Tina want to know it size on the (n-1)-th day modulo n.
Inputthe first line of input contains an integer T , representing the number of cases.
The following T Lines, each line contains an integer n , according to the description.
T ≤ ten 5 ,2≤N≤ ten 9
Outputfor each test case, output an integer representing the answer.
Sample Input
2310
Sample Output
20
Sourcebestcoder Round #51 (Div.2):
the problem is to ask (n?1)! MoD n (n-1)! modn (N?1)!moD N
If nn NFor composite, the answer is obviously 0.
If nnn Is the prime number, then the answer to the Wilson theorem can be< Span class= "Katex-mathml" style= "Position:absolute; padding:0px; border:0px; height:1px; width:1px; Overflow:hidden ">n?1n-1 n 1
Note that there is a trick for nnn = 4
Code:
#include <iostream> #include <cmath> #define LL long long using namespace Std;int t;int n,ans;bool isprime (int m ) { int i,isqrt= (int) sqrt (m); BOOL Pd=true; if (m==2) pd=true; else if (m%2==0) Pd=false; else for (i=3;i<=isqrt;i+=2) { if (m%i==0) { pd=false; break; } } return PD;} int main () { cin>>t; while (t--) { cin>>n; if (n==4) ans=2; else if (IsPrime (n)) ans=n-1; else ans=0; cout<<ans<<endl; } return 0;}
Started to time out, the problem is in the number of judgment prime. We must pay attention to the problem of prime number determination.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu5391 Wilson theorem