Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18511 | Accepted: 11766 |
Description
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
Output
Sample Input
2 5 100
Sample Output
2 10
Source
#include <stdio.h>
int main()
{
int cases;
int n, i, j, open;
int cells[101];
scanf("%d", &cases);
while (cases--) {
scanf("%d", &n);
for (i = 1; i <= n; i++)
cells[i] = 0;
open = n;
for (i = 2; i <= n; i++) {
for (j = i; j <= n; j += i) {
cells[j] = !cells[j];
if (cells[j])
open--;
else
open++;
}
}
printf("%d\n", open);
}
return 0;
}