如果n是素数那么余数就是n-1,如果不是素数对n的余数肯定是0,当然4除外,是2.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define N 100000
#define INF 0xfffffff
int num[N], k;
int a[N] = {1, 1};
void prim()
{
k = 0;
//memset(num, 0, sizeof(num));
for(int i = 2; i < N; i++)
{
if(!a[i])
num[k++] = i;
for(int j = i+i; j < N; j += i)
a[j] = 1;
}
}
int isprime(int n)
{
if(n == 0 || n == 1)
return 0;
for(int i = 0; (long long)num[i]*num[i] <= n; i++)
if(n % num[i] == 0)
return false;
return true;
}
int main()
{
int t, n;
scanf("%d", &t);
prim();
while(t--)
{
scanf("%d", &n);
if(n <= 4)
printf("2\n");
else if(isprime(n))
printf("%d\n", n-1);
else
puts("0");
}
return 0;
}