hdu5391 Zball in Tina Town

Problem Description
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 time as large as its original size. On the second day,it will become 2 times as large as the size on the first day. On the n-th day,it will become  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, representing the number of cases.

The following  T lines, each line contains an integer  n, according to the description.
T105,2n109
 

Output
For each test case, output an integer representing the answer.
 

Sample Input
 
   
2 3 10
 

Sample Output
 
   
2 0

这题求的是(n-1)!%n,根据打表发现,当n是4的时候输出2,其他如果是素数的话就输出n-1,否则输出0.用了线性素数筛法,时间少了很多。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define maxn 400000
int prime[maxn+10],vis[maxn+10],tot;
void init()
{
    int i,j;
    tot=0;
    for(i=2;i<=maxn;i++){
        if(!vis[i]){
            tot++;prime[tot]=i;
        }
        for(j=1;j<=tot &&prime[j]*i<=maxn;j++){
            vis[prime[j]*i]=1;
            if(i%prime[j]==0)break;
        }
    }
}



int main()
{
    int n,m,i,j,num,T,flag;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        if(n==4){
            printf("2\n");continue;
        }
        if(n<=maxn){
            if(!vis[n]){
                printf("%d\n",n-1);
            }
            else printf("0\n");
            continue;
        }
        flag=1;
        for(i=1;i<=tot && prime[i]*prime[i]<=n;i++){
            if(n%prime[i]==0){
                flag=0;break;
            }
        }
        if(flag)printf("%d\n",n-1);
        else printf("0\n");
    }
    return 0;
}


你可能感兴趣的:(hdu,素数,素数)