Tina Town is a friendly place. People there care about each other.
Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes
1
1 time as large as its original size. On the second day,it will become
2
2 times as large as the size on the first day. On the n-th day,it will become
n
n times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day modulo n.
Input
The first line of input contains an integer
T
T, representing the number of cases.
The following
T
T lines, each line contains an integer
n
n, according to the description.
T≤105,2≤n≤109
T≤105,2≤n≤109
Output
For each test case, output an integer representing the answer.
Sample Input
2
3
10
Sample Output
2
0
题意:
求(n-1)!%n
分析:
分合数和素数讨论,如果n是素数,根据威尔逊定理:((p-1)!+1)%p==0,可得(p-1)!%p = p-1;
如果n是合数,当n>4时,(p-1)!一定能整除p,所以(p-1)!%p =0; 特别的当 n = 1,(p-1)!%p=0;当n=4,(p-1)!%p =2;
代码:
#include <stdio.h>
using namespace std;
bool isPrime(int x){//判断x是不是质数,是返回true,不是返回false
if(x <= 1) return false;
for(int i = 2; i * i <= x; i ++){//用乘法避免根号的精度误差
if(x % i == 0) return false;
}
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
if(n==4) printf("2\n");
else if(isPrime(n)) printf("%d\n",n-1);
else printf("0\n");
}
}