poj 3090 Visible Lattice Points 欧拉函数打表求和

Visible Lattice Points
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6329   Accepted: 3774

Description

A lattice point (xy) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (xy) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (xy) with 0 ≤ xy ≤ 5 with lines from the origin to the visible points.

poj 3090 Visible Lattice Points 欧拉函数打表求和_第1张图片

Write a program which, given a value for the size, N, computes the number of visible points (xy) with 0 ≤ xy ≤ N.

Input

The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

Output

For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

Sample Input

4
2
4
5
231

Sample Output

1 2 5
2 4 13
3 5 21
4 231 32549


题意:给出一个n*n的二维空间,求所有点(x,y)使得x,y互质,其中包括(1,0),(1,1),(0,1)三个点

思路:欧拉函数打表求出∑phi(n),乘以二+3即为答案


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
long long p[10006];
void phi(long long N)    
{    
    for(long long i=1; i<N; i++)  p[i] = i;    
    for(long long i=2; i<N; i+=2) p[i] >>= 1;    
    for(long long i=3; i<N; i+=2)    
    {    
        if(p[i] == i)    
        {    
            for(long long j=i; j<N; j+=i)    
                p[j] = p[j] - p[j] / i;    
        }    
    }    
}    
int main(int argc, char const *argv[])
{   long long i,j,k,m,n,t;
	long long sum=0;
	phi(10001);
	scanf("%lld",&t);
    for(k=1;k<=t;k++)
    {   scanf("%lld",&n);
	    sum=0;
    	for(i=2;i<=n;i++)sum+=p[i];
    	printf("%lld %lld %lld\n",k,n,2*sum+3);	

    }
	return 0;
}


你可能感兴趣的:(数论,ACM,poj,欧拉函数)