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


题目分析:一个球初始体积为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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值