数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5636   Accepted: 3317

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

Source

 

Mean:

在第一象限中,输入一个n,然后要你统计在(0<=x<=n,0<=y<=n)的范围内,有多少可视点。

所谓的可视点,即:从(0,0)出发到达(x1,y1),中间未与任何整点相交的点。

analyse:

通过分析,我们会发现:只要x和y互质,那么(x,y)就是可视点。我们只要求得[0,0]~[x,y]内满足x和y互质的点(x,y)的个数,那么问题就可迎刃而解。欧拉函数就是用来解决小于n的数中有多少个数与n互质。

Time complexity:O(n)

Source code:

// Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-12-22.35
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std; int gcd(int a,int b){
return b?gcd(b,a%b):a;
} inline int lcm(int a,int b){
return a/gcd(a,b)*b;
} int eular(int n) ////求1..n-1中与n互质的数的个数
{
int ret=1,i;
for (i=2;i*i<=n;i++)
if (n%i==0){
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
if (n>1)
ret*=n-1;
return ret;
} int main()
{
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
int t,Cas=1;
cin>>t;
while(t--)
{
int n;
cin>>n;
LL ans=0;
for(int i=1;i<=n;i++)
{
ans+=eular(i);
}
printf("%d %d %d\n",Cas++,n,ans*2+1);
}
return 0;
}

  

上一篇:浅谈oracle树状结构层级查询之start with ....connect by prior、level及order by


下一篇:android学习3——长宽的单位问题dp,px,dpi