UESTC1141 A Simple Game

http://acm.uestc.edu.cn/ShowProblem.aspx?ProblemID=1141&ContestID=24

 A Simple Game  
Time Limit:1000ms  Memory Limit: 65535K
Submited:478  Accepted:50
Cached at 2010/3/29 22:49:14
Description
Alice likes to play games. One day she meets such a game. There are N * N switches arranged in an N * N array. Pressing a switch would change its state, from 'off' to 'on' or from 'on' to 'off'. In addition, if a switch at row r and column c is pressed, then all switches with coordinate (k * r, k * c) will change state, with integer k > 1. Initially all switches are 'off'.

For example, in the picture above, white buttons represent switches turned 'off' and colored ones represent switches turned 'on'. Initially all buttons are white. If the button at (2,2) is pressed, then buttons at (2,2), (4,4) will change state(represented with orange color). And if one presses the button (2,1), buttons at (2,1) and (4,2) will change from 'off' to 'on'(represented with gray color).
The goal of the game is to turn 'on' all the switches (i.e. when you finish the game, all the switches must be at the state of 'on') and the player must do that with as few presses as possible. Now Alice would like your help.

 


Input
The first line of input file is an integer T, the number of test cases. T lines follow, each contain an integer N, the dimension of the array in one game.
1 ≤ T ≤ 100, 1 ≤ N ≤ 10000

 


Output
Output consists of T lines. Each line contains an integer, the minimum number of presses for the corresponding test case.

 


Sample Input
2
2
3

Sample Output
3
7

Hint
For test case 1, press (1,1) (1,2) (2,1).
For test case 2, press (1,1) (1,2) (1,3) (2,1) (2,3) (3,1) (3,2).

Source
The 5th UESTC Programming Contest Preliminary
 

/* 计算由其它开关打开的开关数量 递推 (x,y)=(ka,kb) k>1 所以x与y有公约数k n=0,s[n]=0; s[n]=s[n-1]+(2 到 n-1)与n有公约数的个数*2+1 公约数个数由欧拉函数间接求出 ans[n]=n*n-s[n] */ #include<iostream> #define M 100005 using namespace std; int s[M],prime[M],phi[M]; int gcd(int x,int y) {return y?gcd(y,x%y):x;} void init() { int i,j; for(i=4;i<=M;i+=2)//素数筛选 prime[i]=1;//1表示不是素数 for(i=3;i*i<=M;i+=2) if(!prime[i]) for(j=i*i;j<=M;j+=(i<<1)) prime[j]=1; for(i=1;i<=M;i++)//欧拉函数 phi[i]=i; for(i=2;i<=M;i++) if(!prime[i]) for(j=i;j<=M;j+=i) phi[j]=phi[j]/i*(i-1); for(i=2;i<=10000;i++)//(公约数个数-1)*2+1 s[i]=s[i-1]+(i-1-phi[i])*2+1; } int main() { int t,n; init(); scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d/n",n*n-s[n]); } }

你可能感兴趣的:(File,Integer,input,each,button,output)