poj 1218 THE DRUNK JAILER

来源:互联网 发布:opencv python视频教程 编辑:程序博客网 时间:2024/05/19 17:27
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

25100

Sample Output

210

【题意】给你n盏灯,开始的时候都是关闭的,然后把1的倍数的灯点亮,然后把2的倍数的灯点亮,,,然后把n的倍数的灯点亮,问你经过n次操作之后还有多少灯亮着。

【代码】题目简单,先上代码,后面分析。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>using namespace std;int main(){    int a,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&a);        printf("%d\n",(int)sqrt((double)a));    }    return 0;}

【分析】显然答案很简单,只有当x是某一个整数的平方的时候这盏灯能亮。下面来证明一下。

【证明】
先证明 整数的平方的灯->会亮
显然当遇到了该数的因子的时候,这盏灯会改变当前状态。而且显然对于某个数,如果他的所有因子是奇数个,她最后一定会亮。对于整数的平方的灯,他的所有的因子一定是两两配对加上sqrt(n),所以显然整数的平方的灯有奇数个因子,所以最后她会亮。

再证明会亮的灯是整数的平方。
显然亮的灯的因子是奇数个,并且如果a|n那么一定有(n/a)|n,所以因子一定是两两配对,如果因子数是奇数个,说明a==n/a。所以n==a*a。证毕。