Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5239 | Accepted: 3049 |
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
这道题是欧拉公式,给你t组数据,每组数据里有有1个n,建立一个(n+1)*(n+1)的点图,问你从左下角的的点可以连到多少个点。(每条连线不能在途中包含其他点)
看的时候先把从左下到右上的对角线去掉,剩下的两个部分是相同的。拿该图来说,把从左下到右上的对角线去掉后下面的那个三角形是一个5*5的三角形,再用欧拉公式算1-5的和(把1的值看做1),1→1,2→1,3→2,4→2,5→4,可以在图中试着找到对应的点。如第五列第三行对应的1/4,第五列第五行对应的3/4,第四列第四行对应的1/3,第四列第五行对应的2/3。最后结果为(1-n的欧拉和)*2+1(对角线还还有一个)
输出结果为 第t组数据 给你的n数 结果
#include<cstdio>
using namespace std;
int p[1005]; //p[j]表示小于j的正整数与j互斥的数有多少个,比如j=4,那么1/4,3/4共两个
int s[1005]; //s[j]表示从p[2]加到p[j]一共有多少个数
int main()
{ int i,j,t=1,n,k;
p[1]=1; //设p[1]为1
for(i=2;i<=1000;i++) //欧拉公式
{ if(!p[i]) //如果这个数是素数
{ for(j=i;j<=1000;j+=i) //所有能被这个数整除的数
{ if(!p[j]) //没有涉及过的数先初始化一下p[j]的个数
p[j]=j;
p[j]=p[j]/i*(i-1); //都除以i再乘以(i-1)
}
}
}
for(i=1;i<=1000;i++) //算从p[1]加到p[i]一共有多少个数
s[i]=s[i-1]+p[i];
scanf("%d",&n);
while(n--)
{ scanf("%d",&k);
printf("%d %d %d\n",t++,k,s[k]*2+1);
}
}