Poj1218 THE DRUNK JAILER

Description

  A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.
  One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey,and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell (cells 2, 4, 6, ?). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, ?). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out.
  Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.
  Given the number of cells, determine how many prisoners escape jail.
Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.
Output

For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input

2
5
100

Sample Output

2
10


【分析】
如果囚犯最后被释放,就意味着该囚犯牢房的状态被转换了奇数次。比如牢房16,经过了5次状态转换(1,2,4,8,16),因此牢房16的囚犯最终会被释放。
根据题目的含义,只需要找出所有的数,这些数仅有奇数个因子。
考虑这种情况,x的因子有k1,k2,...,kn,其中k1<k2...<kn,且n是奇数。
显然k1kn=1*x=x
另外还有k1kn=k2kn-1=k3kn-2=...kp-1kp+1=x,其中kp是k1,k2,...kn的中间数。
因为kp是x的因子,于是x/kp也是x的因子。
然而x/kp不属于集合{k1,k2,...,kp-1}U{kp+1,...,kn}(用反证法,可得kp属于{k1,k2,...,kp-1}U{kp+1,...,kn},推出矛盾)。
那么x/kp只能等于x,x=kp*kp。得到了x是完全平方数。
结论:完全平方数仅有奇数个因子。

 

#include <math.h>
 
int main()
{
	int count;
	scanf("%d",&count);
	while(count--)
	{
		int number;
		scanf("%d",&number);
		printf("%d\n",(int)sqrt(number)); 
	} 
	return 0; 
} 

 

 

 

posted @ 2012-07-04 10:04  SourceCoder  阅读(177)  评论(0)    收藏  举报