Visible Lattice Points
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
题目类型:法雷级数
算法分析:所有可见的点的坐标(x, y)满足gcd (x, y) = 1,本题的坐标关系恰好可以使用法雷级数来表示,由于坐标位置存在顺序,所以总的个数等于2倍的F(n)再减去多算的对角线上的一个坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 1000 + 66; long long euler[maxn]; void GetEuler () { for (long long i = 0; i < maxn; i++) euler[i] = i; for (long long i = 2; i < maxn; i += 2) euler[i] >>= 1; for (long long i = 3; i < maxn; i += 2) { if (euler[i] == i) { for (long long j = i; j < maxn; j += i) euler[j] = euler[j] - euler[j] / i; } } } int main() { // ifstream cin ("aaa.txt"); GetEuler (); for (long long i = 1; i < maxn; i++) euler[i] += euler[i-1]; long long t, flag = 1; cin >> t; while (t--) { long long val; cin >> val; cout << flag++ << " " << val << " " << euler[val] * 2 + 1<< endl; } return 0; } |
- « 上一篇:poj3061
- poj3122:下一篇 »