hdu5391

来源:互联网 发布:软件自动化研究生 编辑:程序博客网 时间:2024/06/18 16:02
I - Zball in Tina Town
Time Limit:1500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 5391

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

The following  lines, each line contains an integer , according to the description. 
 

Output

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

Sample Input

2310

Sample Output

20
题解
题意 (n-1)!%n
这题很容易可以判断出,如果这个数不是素数,那么答案就是0(除了4)
4比较特殊,因为判断是不是素数的时候,2*2.而在这题里面,分母只有一个2.
接下来就是要处理素数的情况。如果直接计算取模,会超时。
引入数学知识:
威尔逊定理。
初等数论中,威尔逊定理给出了判定一个自然数是否为素数充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ p-1 ( mod p )
#include<stdio.h>#include<algorithm>#include<math.h>#include<iostream>using namespace std;int judge(int x){    if(x==1)        return 0;    for(int i=2; i<=sqrt(x); i++)    {        if(x%i==0)        {           return 1;        }    }    return 0;}int main(){    int ans,t,n,m;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        if(n==4)            printf("2\n");        else if(judge(n)==0)            printf("%d\n",n-1);        else            printf("0\n");    }}


0 0