最大公约数
思路 :
\(gcd(x,y)=p,1\le x,y \le n \Rightarrow gcd(\frac{x}{p},\frac{y}{p})=1 \Rightarrow gcd(x′,y′)=1,1 \le x′,y′\le \frac{n}{p}\)
所以其实很经典的在矩形(n*n)坐标范围下求 \((x,y)\) 的互质对数.只不过这里的 \(n\) 也是变量.
本题扩展于可见的点
#include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0); inline int lowbit(int x) { return x & (-x); } #define ll long long #define ull unsigned long long #define pb push_back #define PII pair<int, int> #define VIT vector<int> #define x first #define y second #define inf 0x3f3f3f3f const int N = 1e7 + 10; int pri[N], cnt; bool st[N]; ll phi[N]; void init() { phi[1] = 1; for (int i = 2; i < N; ++i) { if (!st[i]) { pri[cnt++] = i; phi[i] = i - 1; } for (int j = 0; pri[j] * i < N; ++j) { st[pri[j] * i] = true; if (i % pri[j] == 0) { phi[pri[j] * i] = phi[i] * pri[j]; break; } phi[pri[j] * i] = phi[i] * (pri[j] - 1); } } } int main() { IO; init(); int n; cin >> n; phi[1] = 0; for (int i = 2; i < N; ++i) phi[i] += phi[i - 1]; ll ans = 0; for (int i = 0; pri[i] <= n; ++i) ans += phi[n / pri[i]] * 2 + 1; cout << ans << '\n'; return 0; }