HDU 5391 Zball in Tina Town【威尔逊定理】

时间:2024-09-24 19:03:51

<题目链接>

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 2times 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 mod 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.
T≤105,2≤n≤109
Output
For each test case, output an integer representing the answer.
Sample Input
2
3
10
 
Sample Output
2
解题分析:
此题一看到 (n-1)! 就要立刻想到威尔逊定理,当n为素数时,毫无疑问 (n-1)! %n=n-1 ,但是如果n不是素数,而是合数,应该怎么办呢?我们可以简单的试几个数,如果n为合数,我们发现
(n-1)!中总能够找出一些因子,使它们想乘为n,但是要注意4是个例外,由于4比较靠前,所以(4-1)!不一定能够凑出4,除了这种情况,其他的合数毫无疑问答案都为0。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; int juge(int x)
{
for(int i=;i*i<=x;i++)
{
if(x%i==)return false;
}
return true;
} int main()
{ int t;cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
if(n==)
{
printf("2\n");
}
else
{
if(juge(n))
{
printf("%d\n",n-);
}
else
printf("0\n");
}
}
return ;
}
2018-07-31