hdu-5391-Zball in Tina Town



题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391




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 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
 


题目分析:一个球初始体积为1,一天天变大,第一天变大1倍,第二天变大2倍,第n天变大n倍。问当第 n-1天的时候,体积变为多少。注意答案对n取模。也就是说第一天是1,第二天是1*2,第三天是1*2*3,也就是当第n天的时候是n!。那么答案就是(n-1)! % n。
题目里面n是10^9,直接阶乘肯定不行,找规律发现,如果是素数,答案就是n-1,否则就是0。4是个例外,n=4时输出2,于是问题就转化成为求n是否为素数!!!



#include 
#include 
#include 
#include 
using namespace std;
int prime(int n)
{
	for(int i=2; i*i<=n; i++)
		if(n % i == 0)
			return 0;
	return 1;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n;
		scanf("%d",&n);
		if(n == 4) printf("2\n");
		else if(!prime(n)) printf("0\n");
		else printf("%d\n",n-1);
	}
}


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