Wilson's theorem: When and only if p is a prime number: (p-1)!≡p-1 (mod p) This is a sufficient condition
This problem is done with Wilson's theorem, but don't know the theorem, it doesn't matter, can make a table,
Summary: Some problems of number theory even if you do not know a certain knowledge point, but also try to lay the table, see if you can find the law
#include <cstdio>int main () {for (int i=1;i<=13;i++) { int sum=1; for (int j=1;j<i;j++) sum*=j; printf ("%d\n", sum%i); } return 0;}
Run, you can observe, composite results are 0, prime number results are n-1, there is an exception, that is 4, it also output 2;
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5391
#include <cstdio> #include <cmath>int main () { int t; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); if (n==4) {printf ("2\n"); continue;} if (n==1) {printf ("1\n"); continue;} int flag=1; int M=floor (sqrt (n*1.0) +0.5);//Note that this long expression cannot be placed in the for-judging condition and will time out for (int i=2;i<=m;i++) { if (n%i==0) { flag=0;break; } } if (flag) printf ("%d\n", n-1); else printf ("0\n"); } return 0;}
Copyright NOTICE: Reprint must please indicate the source, thank you
Bestcoder Round #51 (Div.2) Zball in Tina town