求$$\sum_1^n \sum_1^n \phi(gcd(i, j))$$函數
$T \leqslant 5000, N \leqslant 10^7$spa
延用BZOJ4407的作法code
化到最後能夠獲得blog
$$\sum_{T = 1}^n \frac{n}{T} \frac{n}{T} \sum_{d \mid T}^n \phi(d) \mu(\frac{T}{d})$$get
後面的那個是積性函數,直接篩出來it
注意這個函數比較特殊,篩的時候須要分幾種狀況討論io
1. $H(p) = p - 2$ast
2. $H(p^2) = p^2 - 2p + 1$class
3. $H(p^{k + 1}) = H(p^k) * p$gc
// luogu-judger-enable-o2 #include<cstdio> #include<algorithm> #define LL long long using namespace std; const int MAXN = 1e7 + 10, mod = 1e9 + 7; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int prime[MAXN], vis[MAXN], tot; LL H[MAXN], low[MAXN]; void GetH(int N) { H[1] = vis[1] = 1; for(int i = 2; i <= N; i++) { if(!vis[i]) prime[++tot] = i, H[i] = i - 2, low[i] = i; for(int j = 1; j <= tot && i * prime[j] <= N; j++) { vis[i * prime[j]] = 1; if(!(i % prime[j])) { low[i * prime[j]] = low[i] * prime[j]; if(low[i] == i) { if(low[i] == prime[j]) H[i * prime[j]] = (H[i] * prime[j] + 1); else H[i * prime[j]] = H[i] * prime[j]; } else H[i * prime[j]] = H[i / low[i]] * H[low[i] * prime[j]]; break; } H[i * prime[j]] = H[i] * H[prime[j]]; low[i * prime[j]] = prime[j]; } } for(int i = 2; i <= N; i++) H[i] = H[i - 1] + H[i]; } int main() { GetH(1e7 + 5); int T = read(); while(T--) { int N = read(), last; LL ans = 0; for(int i = 1; i <= N; i = last + 1) { last = N / (N / i); ans = ans + 1ll * (N / i) * (N / i) * (H[last] - H[i - 1]); } printf("%lld\n", ans); } return 0; } /* 3 7001 123000 10000000 */