Zball in Tina Town
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2766 Accepted Submission(s): 1295
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 , representing the number of cases.
The following lines, each line contains an integer n , according to the description.
T≤10 5 ,2≤n≤10 9
Output
For each test case, output an integer representing the answer.
Sample Input
2
3
10
Sample Output
2
0
Source
Recommend
hujie | We have carefully selected several similar problems for you: 6349 6348 6347 6346 6345
中文题
问题描述
Tina Town 是一个善良友好的地方,这里的每一个人都互相关心。
Tina有一个球,它的名字叫zball。zball很神奇,它会每天变大。在第一天的时候,它会变大11倍。在第二天的时候,它会变大22倍。在第nn天的时候,它会变大nn倍。
zball原来的体积是11。Tina想知道,zball在第n-1n−1天时的体积对nn取模是多大呢?
Tina是一个蠢蠢的女孩子,当然不会算啦,所以她请你帮她算出这个答案呢。
输入描述
第一行一个正整数TT,表示数据组数
接下来TT行,每行一个正整数nn,意义如题面所述
T \leq 10^5,2 \leq n \leq 10^9T≤10
5
,2≤n≤10
9
输出描述
对于每组数据,输出一个正整数,表示答案。
输入样例
2
3
10
输出样例
2
0
算法分析
本题要求(n-1)!mod n,如果一个个算会超时,我们知道一个定理为威尔逊定理,如果p为素数,(p-1)! ≡ -1(mod p),即(p-1)!mod p =-1=n-1(其实后面+n,-n,一般计算机默认最小自然数)。
所以,如果n是素数,则(n-1)!mod n=n-1;
如果n为合数,则(n-1)!mod n=0;因为每一个合数都存在比它小的约数能组成该合数,所以(n-1)!必定是能够被n整除的,比如12,它能由3*4构成,所以11!必定能被12整除,
但4是一个特例,特殊判断一下就ok。
代码实现:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
int isPrime(int n)
{ //返回1表示判断为质数,0为非质数,在此没有进行输入异常检测
float n_sqrt;
if(n==2 || n==3) return 1;
if(n%6!=1 && n%6!=5) return 0;
n_sqrt=floor(sqrt((float)n));
for(int i=5;i<=n_sqrt;i+=6)
{
if(n%(i)==0 | n%(i+2)==0) return 0;
}
return 1;
}
int main()
{
int n,t;
cin>>t;
while(t--)
{scanf("%d",&n);
if(n==4) printf("%d\n",2);
else if(isPrime(n)==1)
printf("%d\n",n-1);
else
printf("0\n");
}
return 0;
}