POJ3090 Visible Lattice Points (数论:欧拉函数模板)
题目链接:传送门
思路:
所有gcd(x, y) = 1的数对都满足题意,然后还有(1, 0) 和 (0, 1)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstring> using namespace std; const int MAX_N = 1e3 + 5; int prime[MAX_N+1], phi[MAX_N+1]; void getPrime_and_Phi() { memset(prime, 0, sizeof prime); phi[1] = 1; for (int i = 2; i <= MAX_N; i++) { if (!prime[i]) prime[++prime[0]] = i, phi[i] = i-1; for (int j = 1; j <= prime[0] && prime[j] <= MAX_N/i; j++) { prime[i*prime[j]] = 1; phi[i*prime[j]] = phi[i] * (i%prime[j] ? prime[j]-1 : prime[j]); if (i%prime[j] == 0) break; } } } int main() { getPrime_and_Phi(); int C; cin >> C; for (int kase = 1; kase <= C; kase++) { int N; cin >> N; long long ans = 1; for (int i = 1; i <= N; i++) ans += phi[i]*2; cout << kase << ' ' << N << ' ' << ans << endl; } return 0; }