Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
A lattice point (x, y) 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 (x, y) 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 (x, y) with 0 ≤ x, y ≤ 5 with lines from the origin to the visible points.
Write a program which, given a value for the size, N, computes the number of visible points (x, y) with 0 ≤ x, y ≤ 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的网格,任意一点和(0,0)连线,可以组成一条直线,前面的点可以挡住后面的点,问你能看到的点到底有多少个
思路分析:题目实际上就是问在这个网格上有多少种不同的斜率,边上的两点我们先不管,然后将整个正方形分成上三角和下三角两部分,
由对称性,两边可以看到的点的数目肯定一样多,以下三角为例进行研究,我们会发现,对于所有能看到的点,他们有着一个共同的特征,
那就是gcd(x,y)=1,若不为1,则他前面肯定有一个点挡住了这个点,那么本题就转变成了一个求欧拉函数和的简单题目,注意不要将分界线
上的点加,记t=phi[1]+phi[2]+.......+phi[n],则ans=(t-1)*2+1+2=2*t+1
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<iostream> #include<cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; typedef long long ll; const int maxn=1500; int prime[maxn]; int phi[maxn]; bool check[maxn]; int tot; void make_prime() { phi[1]=1; memset(check,true,sizeof(check)); tot=0; for(int i=2;i<=maxn;i++) { if(check[i]) { prime[tot++]=i; phi[i]=i-1; } for(int j=0;j<tot&&i*prime[j]<=maxn;j++) { check[i*prime[j]]=false; if(i%prime[j]==0) { phi[i*prime[j]]=phi[i]*prime[j]; break; } else phi[i*prime[j]]=phi[i]*(prime[j]-1); } } } int kase; int main() { int T; make_prime(); scanf("%d",&T); kase=0; ll num; while(T--) { int n; scanf("%d",&n); ll ans=0; for(int i=1;i<=n;i++) { ans+=phi[i]; } printf("%d %d %lld\n",++kase,n,ans*2+1); } }