Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8207 | Accepted: 5041 |
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
Source
Regionals 2006 >> North America - Greater NY
问题链接:POJ3090 ZOJ2777 UVALive3571 Visible Lattice Points
问题简述:(略)
问题分析:
这是一个欧拉函数问题,占个位置不解释。
n=1时,只能看到<1,0><0,1>和<1,1>,是个特例。打表比较好,可以去除重复计算,节省时间。
同样是打表,区间打表更好。计算欧拉函数值,可以基于筛选法原理,进行区间打表计算。
程序说明:(略)
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* POJ3090 ZOJ2777 UVALive3571 Visible Lattice Points */
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 1000;
int phisum[N + 1];
/* 欧拉函数(筛选法) */
void setphisum()
{
memset(phisum, 0, sizeof(phisum));
for ( int i = 2 ; i <= N ; i ++ )
if ( ! phisum [i] ) {
for ( int j = i ; j <= N ; j += i ) {
if ( ! phisum [j] )
phisum [j ] = j ;
phisum [j] = phisum [j] / i * ( i - 1 ) ;
}
}
phisum[1] = 3;
for(int i = 2; i <= N; i++)
phisum[i] = phisum[i - 1] + 2 * phisum[i];
}
int main()
{
setphisum();
int c, n;
scanf("%d", &c);
for(int t = 1; t <= c; t++) {
scanf("%d", &n);
printf("%d %d %d\n", t, n, phisum[n]);
}
return 0;
}
AC的C++语言程序如下:
/* POJ3090 ZOJ2777 UVALive3571 Visible Lattice Points */
#include <iostream>
#include <stdio.h>
using namespace std;
const int N = 1000;
int sum[N + 1];
/* 欧拉函数 */
int phi(int 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;
}
void maketable()
{
sum[0] = 0;
sum[1] = 3;
for(int i = 2; i <= N; i++)
sum[i] = sum[i-1] + 2 * phi(i);
}
int main()
{
maketable();
int c, n;
scanf("%d", &c);
for(int t = 1; t <= c; t++) {
scanf("%d", &n);
printf("%d %d %d\n", t, n, sum[n]);
}
return 0;
}