Zball in Tina Town
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5391
解题思路:
官方题解:
这题就是求 (n−1)! mod n
如果n为合数,显然答案为0.
如果n为素数,那么由威尔逊定理可得答案为 n−1
注意有个trick为 n = 4
大水题,但直接暴力会超时,不过,规律打个表就出来了。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
int flag = 1;
int t = sqrt(n*1.0);
if(n == 4){
printf("2\n");
continue;
}
for(int i = 2; i <= t; i++)
if(n % i == 0){
printf("0\n");
flag = 0;
break;
}
if(flag)
printf("%d\n",n-1);
}
return 0;
}