P4213 【模板】杜教筛(Sum)
套路推式子
求s(n)=∑i=1nf(i)∑i=1n(f∗g)(i)=∑i=1n∑d∣if(d)g(id)=∑d=1n∑i=1⌊nd⌋f(i)g(d)=∑d=1ng(d)S(⌊nd⌋)=g(1)S(n)+∑d=2ng(d)S(⌊nd⌋)则有g(1)S(n)=∑i=1n(f∗g)(i)−∑d=2ng(d)S(⌊nd⌋)求s(n) = \sum_{i = 1} ^{n}f(i)\\ \sum_{i = 1} ^{n} (f*g)(i)\\ = \sum_{i = 1} ^{n} \sum_{d\mid i} f(d) g({\frac{i}{d}})\\ = \sum_{d = 1} ^{n} \sum_{i = 1} ^{\lfloor\frac{n}{d}\rfloor}f(i)g(d)\\ = \sum_{d = 1} ^{n}g(d) S(\lfloor\frac{n}{d}\rfloor)\\ = g(1)S(n) + \sum_{d = 2} ^{n} g(d) S(\lfloor\frac{n}{d}\rfloor)\\ 则有g(1)S(n) = \sum_{i = 1} ^{n} (f*g)(i) - \sum_{d = 2} ^{n} g(d) S(\lfloor\frac{n}{d}\rfloor) 求s(n)=i=1∑nf(i)i=1∑n(f∗g)(i)=i=1∑nd∣i∑f(d)g(di)=d=1∑ni=1∑⌊dn⌋f(i)g(d)=d=1∑ng(d)S(⌊dn⌋)=g(1)S(n)+d=2∑ng(d)S(⌊dn⌋)则有g(1)S(n)=i=1∑n(f∗g)(i)−d=2∑ng(d)S(⌊dn⌋)
莫比乌斯函数求和
对S(n)=∑i=1Nμ(i)∑i=1n(I∗μ)(i)=∑d=1nS(nd)1=S(n)+∑d=2nS(nd)S(n)=1−∑d=2nS(nd)对S(n) = \sum_{i = 1} ^{N} \mu(i)\\ \sum_{i = 1} ^{n}(I * \mu)(i)\\ = \sum_{d = 1} ^{n} S(\frac{n}{d})\\ 1 = S(n) + \sum_{d = 2} ^{n} S(\frac{n}{d})\\ S(n) = 1 - \sum_{d = 2} ^{n} S(\frac{n}{d}) 对S(n)=i=1∑Nμ(i)i=1∑n(I∗μ)(i)=d=1∑nS(dn)1=S(n)+d=2∑nS(dn)S(n)=1−d=2∑nS(dn)
欧拉函数求和
对S(n)=∑i=1nϕ(i)∑i=1n(I∗ϕ)(i)=∑d=1nS(nd)=S(n)+∑d=2nϕ(nd)S(n)=n(n+1)2−∑d=2nϕ(nd)对S(n) = \sum_{i = 1} ^{n} \phi(i)\\ \sum_{i = 1} ^{n} (I * \phi)(i)\\ = \sum_{d = 1} ^{n} S(\frac{n}{d})\\ = S(n) + \sum_{d = 2} ^{n} \phi(\frac{n}{d})\\ S(n) = \frac{n (n + 1)}{2} - \sum_{d = 2} ^{n} \phi(\frac{n}{d}) 对S(n)=i=1∑nϕ(i)i=1∑n(I∗ϕ)(i)=d=1∑nS(dn)=S(n)+d=2∑nϕ(dn)S(n)=2n(n+1)−d=2∑nϕ(dn)
代码
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}const int N = 5e6 + 10;ll phi[N], mu[N];
int prime[N], cnt = 0;
bool st[N];void init() {phi[1] = mu[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[cnt++] = i;mu[i] = -1;phi[i] = i - 1;}for(int j = 0; j < cnt && 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);mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) mu[i] += mu[i - 1], phi[i] += phi[i - 1];
}unordered_map<int, ll> ans_phi;
unordered_map<int, ll> ans_mu;ll get_phi(ll x) {if(x < N) return phi[x];if(ans_phi[x]) return ans_phi[x];ll ans = x * (x + 1) >> 1;for(ll l = 2, r; l <= x; l = r + 1) {r = x / (x / l);ans -= (r - l + 1) * get_phi(x / l);}return ans_phi[x] = ans;
}ll get_mu(ll x) {if(x < N) return mu[x];if(ans_mu[x]) return ans_mu[x];int ans = 1;for(ll l = 2, r; l <= x; l = r + 1) {r = x / (x / l);ans -= (r - l + 1) * get_mu(x / l);}return ans_mu[x] = ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();int T = read();while(T--) {ll n = read();printf("%lld %lld\n", get_phi(n), get_mu(n));}return 0;
}