POJ 3090-Visible Lattice Points(罗选法求欧拉函数)

POJ 3090-Visible Lattice Points(筛选法求欧拉函数)

Visible Lattice Points
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3090
Appoint description: 

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(罗选法求欧拉函数)

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+1)*(N+1)的点阵,问从(0,0)点到(x,y)点有多少不被覆盖的直线(相当于斜率不同)

思路:这个相当于就是求<=n的互质数,因为但凡有斜率相同的情况,两个点之间肯定存在有约数的情况,只要约数不同那么斜率肯定不同。这个图的左上和右下是对称的,所以只要求出一半然后*2+1就好了,那个1是对角线的。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
int phi[10010];
int res[10010];
void Euler()
{
    int i,j;
    memset(phi,0,sizeof(phi));
    phi[1]=1;
    for(i=2;i<=10010;i++)
    {
        if(!phi[i])
        {
            for(j=i;j<=10010;j+=i)
            {
                if(!phi[j])
                    phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
    for(i=2;i<=10010;i++)
        phi[i]+=phi[i-1];
}
int main()
{
    int T,n;
    int icase;
    Euler();
    memset(res,0,sizeof(res));
    for(int i=1;i<10010;i++)
        res[i]=phi[i]*2+1;
    scanf("%d",&T);
    for(icase=1;icase<=T;icase++){
        scanf("%d",&n);
        printf("%d %d %d\n",icase,n,res[n]);
    }
    return 0;
}