Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=5391
The Wilson theorem in number theory, the topic directly gives the language description of the expression of Wilson's theorem.
In elementary number theory, the Wilson theorem gives a sufficient and necessary condition for determining whether a natural count is a prime. That is: when and only if P is a prime number: (P-1)! ≡-1 (mod p), but because factorial is exploding, its conclusion is of little significance for practical operation.
The subject of special attention is n=4 time, to special sentence, is a trick. cout, too, will be timed out. Also, the subject data to the 2<=n<=10^9, need to consider is if the table, the complex is O (nsqrt (n)), if the separate sentence is O (n), so do not choose to hit the table. The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<map>#include<Set>#include<cmath>#include<vector>#include<deque>#include<queue>using namespaceStd;typedefLong LongLL;intMain () {intT; scanf ("%d", &T); while(t--) { intFlag =0; intN; scanf ("%d", &N); intm =int(sqrt (n)); for(inti =2; I < m; i++) { if(n% i = =0) {flag++; Break; } } if(n = =0|| n = =1) {printf ("1\n"); } Else if(n = =2) {printf ("2\n"); } Else if(!flag) {printf ("%d\n", N-1); } Else{printf ("0\n"); } }}
View Code
[HDOJ5391] Zball in Tina town