Lady Layton with Math(杜教筛)
Lady Layton with Math
https://ac.nowcoder.com/acm/problem/200008
Lady Layton with Math
/* Author : lifehappy */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e6 + 10, mod = 1e9 + 7; int prime[N], cnt, n; ll phi[N]; bool st[N]; void init() { phi[1] = 1; for(int i = 2; i < N; i++) { if(!st[i]) { prime[++cnt] = i; phi[i] = i - 1; } for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) { st[i * prime[j]] = 1; if(i % prime[j] == 0) { phi[i * prime[j]] = phi[i] * prime[j]; break; } phi[i * prime[j]] = phi[i] * (prime[j] - 1); } } for(int i = 1; i < N; i++) { phi[i] = (phi[i] + phi[i - 1]) % mod; } } unordered_map<int, ll> ans_s; ll Djs(int n) { if(n < N) return phi[n]; if(ans_s.count(n)) return ans_s[n]; ll ans = 1ll * n * (n + 1) / 2 % mod; for(int l = 2, r; l <= n; l = r + 1) { r = n / (n / l); ans = (ans - (r - l + 1) * Djs(n / l) % mod + mod) % mod; } return ans_s[n] = ans; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; init(); scanf("%d", &T); while(T--) { ll ans = 0; scanf("%d", &n); for(int l = 1, r; l <= n; l = r + 1) { r = n / (n / l); ans = (ans + 1ll * (Djs(r) - Djs(l - 1)) * (2ll * Djs(n / l) - 1) % mod + mod) % mod; } printf("%lld\n", ans); } return 0; }