莫比乌斯,欧拉函数题目练习(完结)

Starttime:2020/11/16Start\ time:2020/11/16Start time2020/11/16

Lastupdatetime:2020/11/28Last\ update\ time: 2020/11/28Last update time:2020/11/28

AC22/22AC\ 22 / 22AC 22/22

解方程

∑d∣nf(d)σp(nd)=σq(n)f∗σp=σq有σk=∑d∣ndk=idk∗If∗idp∗I=idq∗I∑d∣nμ(d)=μ∗I对上面式子同时卷上一个μf∗idp=idq因为idk是一个完全积性函数,所以idp−1=μ×idpidk∗(μ×idp)=∑d∣ndk×μ(nd)×(nd)k=∑d∣nμ(d)=ϵf=idq∗(μ×idp)f(n)=∑d∣nμ(d)×dp×(nd)qf(1)=1f(n)=nq−np(n∈primes)f(ab)=f(a)×f(b)(gcd(a,b)=1)f(nx)=nq−npn(x−1)q=nxq−np+(x−1)q(n∈primes)然后就可以线性筛了\sum_{d \mid n} f(d) \sigma_{p}(\frac{n}{d}) = \sigma_{q}(n)\\ f * \sigma_{p} = \sigma_{q}\\ 有\sigma_{k} = \sum_{d \mid n} d ^{k} = id_{k} * I\\ f * id_{p} * I = id_{q} * I\\ \sum_{d \mid n} \mu(d) = \mu * I\\ 对上面式子同时卷上一个\mu\\ f * id_{p} = id_{q}\\ 因为id_{k}是一个完全积性函数,所以id_{p} ^{-1} = \mu \times id_{p}\\ id_{k} * (\mu \times id_{p}) = \sum_{d \mid n} d ^k \times \mu(\frac{n}{d}) \times (\frac{n}{d}) ^k = \sum_{d \mid n} \mu(d) = \epsilon\\ f = id_{q} * (\mu \times id_{p})\\ f(n) = \sum_{d \mid n} \mu(d) \times d ^ p \times (\frac{n}{d}) ^{q}\\ f(1) = 1\\ f(n) = n ^ q - n ^ p(n \in primes)\\ f(ab) = f(a) \times f(b) (gcd(a, b) = 1)\\ f(n ^ x) = n ^ q - n ^ p n ^{(x - 1)q} = n ^ {xq} - n ^{p + (x - 1) q} (n \in primes)\\ 然后就可以线性筛了\\ dnf(d)σp(dn)=σq(n)fσp=σqσk=dndk=idkIfidpI=idqIdnμ(d)=μIμfidp=idqidkidp1=μ×idpidk(μ×idp)=dndk×μ(dn)×(dn)k=dnμ(d)=ϵf=idq(μ×idp)f(n)=dnμ(d)×dp×(dn)qf(1)=1f(n)=nqnp(nprimes)f(ab)=f(a)×f(b)(gcd(a,b)=1)f(nx)=nqnpn(x1)q=nxqnp+(x1)q(nprimes)线

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e7 + 10, mod = 998244353;ll prime[N], minnp[N], f[N], cnt, p, q, n;bool st[N];ll quick_pow(ll a, int n) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}void init() {f[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;f[i] = (quick_pow(i, q) - quick_pow(i, p) + mod) % mod;minnp[i] = 1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {minnp[i * prime[j]] = minnp[i] + 1;f[i * prime[j]] = f[i / quick_pow(prime[j], minnp[i])] * (quick_pow(prime[j], q * (minnp[i * prime[j]])) - quick_pow(prime[j], p + minnp[i] * q) + mod) % mod;break;}minnp[i * prime[j]] = 1;f[i * prime[j]] = f[i] * f[prime[j]] % mod;}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> n >> p >> q;init();ll ans = 0;for(int i = 1; i <= n; i++) {ans ^= f[i];}cout << ans << "\n";return 0;
}

求和

f(n)=∑i=1n∑j=1ngcd(i,j,n)f(n)=∑d∣nd∑i=1nd∑j=1nd[gcd(i,j,nd)=1]∑d∣nd∑k∣ndμ(k)nkdnkdT=kd∑T∣nnTnT∑d∣Tdμ(Td)∑T∣nnTnTϕ(T)∑i=1n∑d∣iididϕ(d)∑d=1nϕ(d)∑i=1ndi2f(n) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} gcd(i, j, n)\\ f(n) = \sum_{d \mid n} d \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}[gcd(i, j, \frac{n}{d}) = 1]\\ \sum_{d \mid n} d \sum_{k \mid \frac{n}{d}} \mu(k) \frac{n}{kd} \frac{n}{kd}\\ T = kd\\ \sum_{T \mid n} \frac{n}{T} \frac{n}{T} \sum_{d \mid T} d \mu(\frac{T}{d})\\ \sum_{T \mid n} \frac{n}{T} \frac{n}{T} \phi(T)\\ \sum_{i = 1} ^{n} \sum_{d \mid i} \frac{i}{d} \frac{i}{d} \phi(d)\\ \sum_{d = 1} ^{n} \phi(d) \sum_{i = 1} ^{\frac{n}{d}} i ^ 2\\ f(n)=i=1nj=1ngcd(i,j,n)f(n)=dndi=1dnj=1dn[gcd(i,j,dn)=1]dndkdnμ(k)kdnkdnT=kdTnTnTndTdμ(dT)TnTnTnϕ(T)i=1ndididiϕ(d)d=1nϕ(d)i=1dni2

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10;int prime[N], phi[N], cnt, n, mod, inv6;bool st[N];int quick_pow(int a, int n) {int ans = 1;while(n) {if(n & 1) ans = 1ll * ans * a % mod;a = 1ll * a * a % mod;n >>= 1;}return ans;
}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, int> ans_s;int Djs_phi(int n) {if(n < N) return phi[n];if(ans_s.count(n)) return ans_s[n];int ans = 1ll * n * (n + 1) / 2 % mod;for(int l = 2, r; l <= n; l = r + 1) {r = n / (n / l);ans = (ans - 1ll * (r - l + 1) * Djs_phi(n / l) % mod + mod) % mod;}return ans_s[n] = ans;
}int calc(int n) {return 1ll * n * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &mod);init();inv6 = quick_pow(6, mod - 2);int ans = 0;for(int l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans = (ans + 1ll * (Djs_phi(r) - Djs_phi(l - 1)) * calc(n / l) % mod + mod) % mod;}printf("%d\n", ans);return 0;
}

算术

∑i=1n∑j=1mμ(lcm(i,j))∑i=1n∑j=1mμ(igcd(i,j))μ(j)∑i=1n∑j=1mμ(i)μ(j)μ(gcd(i,j))∑d=1nμ(d)∑i=1ndμ(id)∑j=1mdμ(jd)[gcd(i,j)=1]∑d=1nμ(d)∑k=1ndμ(k)∑i=1nkdμ(ikd)∑j=1mkdμ(jkd)T=kd∑T=1n∑i=1nTμ(iT)∑j=1mTμ(jT)∑d∣Tμ(d)×μ(Td)\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \mu(lcm(i, j))\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \mu(\frac{i}{gcd(i, j)}) \mu(j)\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \mu(i) \mu(j) \mu(gcd(i, j))\\ \sum_{d = 1} ^{n} \mu(d) \sum_{i = 1} ^{\frac{n}{d}} \mu(id) \sum_{j = 1} ^{\frac{m}{d}} \mu(jd)[gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} \mu(d) \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \sum_{i = 1} ^{\frac{n}{kd}} \mu(ikd) \sum_{j = 1} ^{\frac{m}{kd}} \mu(jkd)\\ T = kd\\ \sum_{T = 1} ^{n} \sum_{i = 1} ^{\frac{n}{T}} \mu(iT) \sum_{j = 1} ^{\frac{m}{T}} \mu(jT) \sum_{d \mid T} \mu(d) \times \mu(\frac{T}{d})\\ i=1nj=1mμ(lcm(i,j))i=1nj=1mμ(gcd(i,j)i)μ(j)i=1nj=1mμ(i)μ(j)μ(gcd(i,j))d=1nμ(d)i=1dnμ(id)j=1dmμ(jd)[gcd(i,j)=1]d=1nμ(d)k=1dnμ(k)i=1kdnμ(ikd)j=1kdmμ(jkd)T=kdT=1ni=1Tnμ(iT)j=1Tmμ(jT)dTμ(d)×μ(dT)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10;int prime[N], mu[N], g[N], f1[N], f2[N], n, m, cnt;bool st[N];void init() {mu[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;mu[i] = -1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {g[j] += mu[i] * mu[j / i];}}
}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;scanf("%d", &T);while(T--) {scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++) {for(int j = i; j <= n; j += i) {f1[i] += mu[j];}}for(int i = 1; i <= m; i++) {for(int j = i; j <= m; j += i) {f2[i] += mu[j];}}int ans = 0;for(int i = 1; i <= n; i++) {ans += f1[i] * f2[i] * g[i];}printf("%d\n", ans);for(int i = 1; i <= n; i++) {f1[i] = 0;}for(int i = 1; i <= m; i++) {f2[i] = 0;}}return 0;
}

Mophues

∑i=1n∑j=1mf(gcd(i,j))∑d=1nf(d)∑i=1nd∑j=1md[gcd(i,j)=1]∑d=1nf(d)∑k=1ndμ(k)nkdmkd∑T=1nnTmT∑d∣T[f(d)<=p]μ(Td)对于所有输入的p,我们按照从小到大排序,对所有的f(d)从小到大排序用一个树状数组来维护∑d∣T[f(d)<=p]μ(Td)\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} f(gcd(i, j))\\ \sum_{d = 1} ^{n} f(d) \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}}[gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} f(d) \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \frac{n}{kd} \frac{m}{kd}\\ \sum_{T = 1} ^{n} \frac{n}{T} \frac{m}{T} \sum_{d \mid T} [f(d) <= p] \mu(\frac{T}{d})\\ 对于所有输入的p,我们按照从小到大排序,对所有的f(d)从小到大排序\\ 用一个树状数组来维护\sum_{d \mid T} [f(d) <= p] \mu(\frac{T}{d})\\ i=1nj=1mf(gcd(i,j))d=1nf(d)i=1dnj=1dm[gcd(i,j)=1]d=1nf(d)k=1dnμ(k)kdnkdmT=1nTnTmdT[f(d)<=p]μ(dT)pf(d)dT[f(d)<=p]μ(dT)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;#define int long longtypedef long long ll;const int N = 5e5 + 10;int prime[N], mu[N], cnt;bool st[N];struct F {int f, id;bool operator < (const F &t) const {return f < t.f;}
}a[N];void init() {a[1].id = mu[1] = 1, a[1].f = 0;for(int i = 2; i < N; i++) {a[i].id = i;if(!st[i]) {prime[++cnt] = i;mu[i] = -1;a[i].f = 1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;a[i * prime[j]].f = a[i].f + 1;if(i % prime[j] == 0) {break;}mu[i * prime[j]] = -mu[i];}}
}struct Ask {int n, m, p, id;bool operator < (const Ask &t) const {return p < t.p;}
}ask[N];int ans[N], tree[N];int lowbit(int x) {return x & (-x);
}void update(int x, int value) {while(x < N) {tree[x] += value;x += lowbit(x);}
}int query(int x) {int ans = 0;while(x) {ans += tree[x];x -= lowbit(x);}return ans;
}signed 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;scanf("%lld", &T);for(int i = 1; i <= T; i++) {scanf("%lld %lld %lld", &ask[i].n, &ask[i].m, &ask[i].p);ask[i].id = i;if(ask[i].n > ask[i].m) {swap(ask[i].n, ask[i].m);}}sort(a + 1, a + N);sort(ask + 1, ask + 1 + T);int now = 1;for(int i = 1; i <= T; i++) {while(now < N && a[now].f <= ask[i].p) {for(int d = a[now].id; d < N; d += a[now].id) {update(d, mu[d / a[now].id]);}now++;}int res = 0;for(int l = 1, r; l <= ask[i].n; l = r + 1) {r = min(ask[i].n / (ask[i].n / l), ask[i].m / (ask[i].m / l));res += (ask[i].n / l) * (ask[i].m / l) * (query(r) - query(l - 1));}ans[ask[i].id] = res;}for(int i = 1; i <= T; i++) {printf("%lld\n", ans[i]);}return 0;
}

GuGuFishtion(???)

∑i=1n∑j=1mϕ(ij)ϕ(i)ϕ(j)ϕ(ij)=ϕ(i)ϕ(j)ϕ(gcd(i,j))gcd⁡(i,j)∑i=1n∑j=1mgcd(i,j)ϕ(gcd(i,j))∑d=1ndϕ(d)∑i=1nd∑j=1md[gcd(i,j)=1]∑d=1ndϕ(d)∑k=1ndμ(k)nkdmkd\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \frac{\phi(ij)}{\phi(i) \phi(j)}\\ \phi(ij) = \frac{\phi(i) \phi(j)}{\phi(gcd(i, j))} \gcd(i, j)\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \frac{gcd(i, j)}{\phi(gcd(i, j))}\\ \sum_{d = 1} ^{n} \frac{d}{\phi(d)} \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}}[gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} \frac{d}{\phi(d)} \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \frac{n}{kd} \frac{m}{kd}\\ i=1nj=1mϕ(i)ϕ(j)ϕ(ij)ϕ(ij)=ϕ(gcd(i,j))ϕ(i)ϕ(j)gcd(i,j)i=1nj=1mϕ(gcd(i,j))gcd(i,j)d=1nϕ(d)di=1dnj=1dm[gcd(i,j)=1]d=1nϕ(d)dk=1dnμ(k)kdnkdm

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 2e6 + 10;int prime[N], phi[N], mu[N], inv[N], n, m, mod, cnt;ll sum[N];bool st[N];ll quick_pow(ll a, int n) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}void init() {memset(st, 0, sizeof st);cnt = 0;mu[1] = phi[1] = inv[1] = 1;for(int i = 2; i < N; i++) {inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;if(!st[i]) {prime[++cnt] = i;mu[i] = -1;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];mu[i * prime[j]] = 0;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] + mu[i] + mod) % mod;sum[i] = (sum[i - 1] + 1ll * i * inv[phi[i]] % mod) % mod;}
}ll calc(int n, int m) {if(n > m) swap(n, m);ll ans = 0;for(int l = 1, r; l <= n; l = r + 1) {r = min(n / (n / l), m / (m / l));ans = (ans + 1ll * (mu[r] - mu[l - 1]) * (n / l) % mod * (m / l) % mod + mod) % mod;}return 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;scanf("%d", &T);while(T--) {scanf("%d %d %d", &n, &m, &mod);init();ll ans = 0;if(n > m) swap(n, m);for(int l = 1, r; l <= n; l = r + 1) {r = min(n / (n / l), m / (m / l));ans = (ans + 1ll * (sum[r] - sum[l - 1]) * calc(n / l, m / l) % mod + mod) % mod;}printf("%lld\n", ans);}return 0;
}

The Boss on Mars

∑i=1ni4[gcd(i,n)=1]∑d∣nμ(d)d4∑i=1ndi4\sum_{i = 1} ^{n} i ^ 4[gcd(i, n) = 1]\\ \sum_{d \mid n} \mu(d) d ^ 4 \sum_{i = 1} ^{\frac{n}{d}}i ^ 4\\ i=1ni4[gcd(i,n)=1]dnμ(d)d4i=1dni4

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10, mod = 1e9 + 7;int prime[N], cnt;bool st[N];void init() {for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}}}
}int mu(int n) {int sum = 0;for(int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= n; i++) {if(n % prime[i] == 0) {n /= prime[i];sum++;if(n % prime[i] == 0) {return 0;}}}if(n != 1) sum++;return sum & 1 ? -1 : 1;
}ll calc1(ll n) {ll ans = ans = (1ll * 6 * n % mod * n % mod * n % mod * n % mod * n % mod + 1ll * 15 * n % mod * n % mod * n % mod * n % mod + 1ll * 10 * n % mod * n % mod * n % mod - n + mod) % mod;ans = ans * 233333335 % mod;return 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--) {int n;scanf("%d", &n);ll ans = 0;for(int i = 1; 1ll * i * i <= n; i++) {if(n % i == 0) {int temp = mu(i);if(temp) {ans = (ans + 1ll * temp * i % mod * i % mod * i % mod * i % mod * calc1(n / i) % mod + mod) % mod;}temp = mu(n / i);if(i * i != n && temp) {ans = (ans + 1ll * temp * (n / i) % mod * (n / i) % mod * (n / i) % mod * (n / i) % mod  % mod * calc1(i) % mod + mod) % mod;}}}printf("%lld\n", ans);}return 0;
}

Spare Tire

乱搞后得到一个结论ai=i2+ia_i = i ^ 2 + iai=i2+i
∑i=1n(i2+i)[gcd(i,m)=1]∑d∣mμ(d)d2∑i=1ndi2+∑d∣mμ(d)d∑i=1ndi接下来分解质因子,然后暴力统计即可,复杂度最多不过O(T×1000)\sum_{i = 1} ^{n} (i ^ 2 + i)[gcd(i, m) = 1]\\ \sum_{d \mid m} \mu(d) d^2\sum_{i = 1} ^{\frac{n}{d}} i ^ 2 + \sum_{d \mid m} \mu(d)d \sum_{i = 1} ^{\frac{n}{d}}i\\ 接下来分解质因子,然后暴力统计即可,复杂度最多不过O(T \times 1000)\\ i=1n(i2+i)[gcd(i,m)=1]dmμ(d)d2i=1dni2+dmμ(d)di=1dniO(T×1000)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10, mod = 1e9 + 7, inv6 = (mod + 1) / 6;int prime[N], fac[N], tot, cnt, n, m;bool st[N];void init() {for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}}}
}ll ans = 0;ll calc1(ll n) {return n * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod;
}ll calc2(ll n) {return n * (n + 1) / 2 % mod;
}void dfs(int sum, int num, int pos) {if(pos > tot) {ll temp = num & 1 ? -1 : 1;ans = ans + temp * sum * sum % mod * calc1(n / sum) % mod + temp * sum * calc2(n / sum) % mod;ans = (ans % mod + mod) % mod;return ;}dfs(sum, num, pos + 1);dfs(sum * fac[pos], num + 1, pos + 1);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();while(scanf("%d %d", &n, &m) != EOF) {tot = 0;for(int i = 1; 1ll * prime[i] * prime[i] <= m; i++) {if(m % prime[i] == 0) {fac[++tot] = prime[i];while(m % prime[i] == 0) {m /= prime[i];}}}if(m != 1) {fac[++tot] = m;}ans = 0;dfs(1, 0, 1);printf("%lld\n", ans);}return 0;
}

整数对

∑i=1n∑j=1m[p∣i×j]∑i=1n∑j=1m[p∣gcd⁡(i,p)×gcd⁡(j,p)]∑d∣p∑i=1n[gcd(i,p)=d]∑k∣p∑j=1m[gcd(j,p)=k][p∣k×d]∑d∣p∑i=1nd[gcd(i,pd)=1]∑k∣p∑j=1mk[gcd(j,pk)=1][p∣k×d]∑d∣p∑x∣pdμ(x)nxd∑k∣p∑y∣pkμ(y)myk[p∣k×d]先预处理出所有的因子来然后就可以暴力统计答案了复杂度好像是O(T×100×100×100)但是跑不满,最多106左右\sum_{i = 1} ^{n} \sum_{j = 1} ^{m}[p \mid i \times j]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{m}[p \mid \gcd(i, p) \times \gcd(j, p)]\\ \sum_{d \mid p} \sum_{i = 1} ^{n}[gcd(i, p) = d] \sum_{k \mid p} \sum_{j = 1} ^{m}[gcd(j, p) = k][p \mid k \times d]\\ \sum_{d \mid p}\sum_{i = 1} ^{\frac{n}{d}}[gcd(i, \frac{p}{d}) = 1]\sum_{k \mid p} \sum_{j = 1} ^{\frac{m}{k}}[gcd(j, \frac{p}{k}) = 1][p \mid k \times d]\\ \sum_{d \mid p} \sum_{x \mid \frac{p}{d}} \mu(x) \frac{n}{xd}\sum_{k \mid p} \sum_{y \mid \frac{p}{k}} \mu(y) \frac{m}{yk}[p \mid k \times d]\\ 先预处理出所有的因子来然后就可以暴力统计答案了\\ 复杂度好像是O(T \times 100 \times 100 \times 100)但是跑不满,最多10 ^ 6左右\\ i=1nj=1m[pi×j]i=1nj=1m[pgcd(i,p)×gcd(j,p)]dpi=1n[gcd(i,p)=d]kpj=1m[gcd(j,p)=k][pk×d]dpi=1dn[gcd(i,dp)=1]kpj=1km[gcd(j,kp)=1][pk×d]dpxdpμ(x)xdnkpykpμ(y)ykm[pk×d]O(T×100×100×100)106

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10;int prime[N], mu[N], cnt, n, m, p;bool st[N];vector<int> fac[N];void init() {mu[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;mu[i] = -1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {fac[j].push_back(i);}}
}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;scanf("%d", &T);while(T--) {scanf("%d %d %d", &n, &m, &p);ll ans = 0;for(auto d : fac[p]) {ll ans1 = 0;for(auto x : fac[p / d]) {ans1 += mu[x] * (n / x / d);}for(auto k : fac[p]) {ll ans2 = 0;if(1ll * k * d % p) continue;for(auto y : fac[p / k]) {ans2 += mu[y] * (m / y / k);}ans += ans1 * ans2;}}printf("%lld\n", ans);}return 0;
}

gcd

∑i=1n∑j=1ngcd(xi−1,xj−1)∑i=1n∑j=1nxgcd(i,j)−1∑d=1n(xd−1)∑i=1nd∑j=1nd[gcd(i,j)=1]∑d=1n(xd−1)(2∑i=1nd∑j=1i[gcd(i,j)=1]−1)∑d=1n(xd−1)(2∑i=1ndϕ(i)−1)\sum_{i = 1} ^{n} \sum_{j = 1} ^{n}gcd(x ^ i - 1, x ^ j - 1)\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} x ^{gcd(i, j)} - 1\\ \sum_{d = 1} ^{n} (x ^ d - 1) \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}[gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} (x ^ d - 1) \left(2\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{i}[gcd(i, j) = 1] - 1\right)\\ \sum_{d = 1} ^{n} (x ^ d - 1) \left(2\sum_{i = 1} ^{\frac{n}{d}}\phi(i) - 1\right)\\ i=1nj=1ngcd(xi1,xj1)i=1nj=1nxgcd(i,j)1d=1n(xd1)i=1dnj=1dn[gcd(i,j)=1]d=1n(xd1)2i=1dnj=1i[gcd(i,j)=1]1d=1n(xd1)2i=1dnϕ(i)1

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10, mod = 1e9 + 7;int prime[N], phi[N], cnt;ll sum[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 - 1] + phi[i]) % mod;}
}ll quick_pow(ll a, int n) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}ll inv(ll x) {return quick_pow(x, mod - 2);
}ll calc(ll x, int l, int r) {if(x == 1) return 0;return (quick_pow(x, l) * inv(x - 1) % mod * (quick_pow(x, r - l + 1) - 1) % mod - (r - l + 1) + mod) % mod;
}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 x, n, ans = 0;scanf("%lld %lld", &x, &n);for(ll l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans = (ans + calc(x, l, r) * (2ll * phi[n / l] - 1) % mod) % mod;}printf("%lld\n", ans);}return 0;
}

Clarke and math

g(i)=∑i1∣i∑i2∣i2∑i3∣i2⋯∑ik∣ik−1f(ik)迪利克雷卷积的定义有f∗g(n)=∑d∣nf(d)f(nd)I∗f=∑d∣nf(d)所以g(i)=(I1∗I2∗I3…Ik−1∗Ik)∗f所以只要做一下迪利克雷卷积的快速幂就好了整体复杂度nlog⁡nlog⁡kg(i) = \sum_{i_1 \mid i} \sum_{i_2 \mid i_2} \sum_{i_3 \mid i_2} \dots\sum_{i_k \mid i_{k - 1}}f(i_k)\\ 迪利克雷卷积的定义有f * g(n) = \sum_{d \mid n} f(d) f(\frac{n}{d})\\ I * f = \sum_{d \mid n} f(d)\\ 所以g(i) = (I_1 * I_2 * I_3 \dots I_{k - 1} * I_{k}) * f\\ 所以只要做一下迪利克雷卷积的快速幂就好了\\ 整体复杂度n \log n \log k\\ g(i)=i1ii2i2i3i2ikik1f(ik)fg(n)=dnf(d)f(dn)If=dnf(d)g(i)=(I1I2I3Ik1Ik)fnlognlogk

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10, mod = 1e9 + 7;int n, k;struct matrix {ll a[N];void init() {for(int i = 1; i <= n; i++) {a[i] = 0;}}void print() {for(int i = 1; i <= n; i++) {printf("%d%c", a[i], i == n ? '\n' : ' ');}}
}a, ans, I;matrix operator * (const matrix & a, const matrix & b) {matrix ans;ans.init();for(int i = 1; i <= n; i++){for(int j = i; j <= n; j += i) {ans.a[j] = (ans.a[j] + a.a[i] * b.a[j / i] % mod) % mod;}}return 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;scanf("%d", &T);while(T--) {scanf("%d %d", &n, &k);for(int i = 1; i <= n; i++) {scanf("%d", &a.a[i]);I.a[i] = 1;}ans.init();ans.a[1] = 1;while(k) {if(k & 1) ans = ans * I;I = I * I;k >>= 1;}ans = ans * a;ans.print();}return 0;
}

Code

n=10000∑i=1n∑j=1ngcd(i,j)×(gcd(i,j)−1)×cnti×cntj∑i=1n∑j=1ngcd(i,j)2×cnti×cntj∑d=1nd2∑i=1ndcntid∑j=1ndcntjd[gcd(i,j)=1]∑d=1nd2∑k=1ndμ(k)(∑i=1nkdcntikd)2T=kd∑T=1n(∑i=1nTcntiT)2∑d∣Td2μ(Td)∑i=1n∑j=1ngcd(i,j)×cnti×cntj同理可得∑T=1n(∑i=1nTcntiT)2∑d∣Tdμ(Td)∑T=1n(∑i=1nTcntiT)2∑d∣T(d2−d)μ(Td)n = 10000\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n}gcd(i, j) \times(gcd(i, j) - 1) \times cnt_i \times cnt_j\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} gcd(i, j) ^ 2 \times cnt_i \times cnt_j\\ \sum_{d = 1} ^{n} d ^ 2 \sum_{i = 1} ^{\frac{n}{d}} cnt_{id} \sum_{j = 1} ^{\frac{n}{d}} cnt_{jd}[gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} d ^ 2 \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \left(\sum_{i = 1} ^{\frac{n}{kd}}cnt_{ikd} \right) ^ 2\\ T = kd\\ \sum_{T = 1} ^{n} \left(\sum_{i = 1} ^{\frac{n}{T}} cnt_{iT} \right) ^ 2 \sum_{d \mid T} d ^ 2 \mu(\frac{T}{d})\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} gcd(i, j) \times cnt_i \times cnt_j同理可得\\ \sum_{T = 1} ^{n} \left(\sum_{i = 1} ^{\frac{n}{T}} cnt_{iT} \right) ^ 2\sum_{d \mid T} d \mu(\frac{T}{d})\\ \sum_{T = 1} ^{n} \left(\sum_{i = 1} ^{\frac{n}{T}}cnt_{iT} \right) ^ 2 \sum_{d \mid T} (d ^ 2 - d) \mu(\frac{T}{d})\\ n=10000i=1nj=1ngcd(i,j)×(gcd(i,j)1)×cnti×cntji=1nj=1ngcd(i,j)2×cnti×cntjd=1nd2i=1dncntidj=1dncntjd[gcd(i,j)=1]d=1nd2k=1dnμ(k)i=1kdncntikd2T=kdT=1ni=1TncntiT2dTd2μ(dT)i=1nj=1ngcd(i,j)×cnti×cntjT=1ni=1TncntiT2dTdμ(dT)T=1ni=1TncntiT2dT(d2d)μ(dT)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e4 + 10, mod = 1e4 + 7;int prime[N], mu[N], num[N], cnt, n;ll g[N], f[N];bool st[N];void init() {mu[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;mu[i] = -1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) {for(int j = i; j <= N; j += i) {f[j] = (f[j] + (1ll * i * i - i) * mu[j / i] % mod + mod) % mod;}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();while(scanf("%d", &n) != EOF) {memset(g, 0, sizeof g);memset(num, 0, sizeof num);for(int i = 1; i <= n; i++) {int x;scanf("%d", &x);num[x]++;}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {g[i] = (g[i] + num[j]) % mod;}}ll ans = 0;for(int i = 1; i < N; i++) {ans =( ans + g[i] * g[i] % mod * f[i]) % mod;}printf("%lld\n", ans);}return 0;
}

AT5200 [AGC038C] LCMs

∑i=0n−2∑j=i+1n−1lcm(ai,aj)更改下标从1开始∑i=1n−1∑j=i+1nlcm(ai,aj)(∑i=1n∑j=1nlcm(ai,aj)−∑i=1nlcm(ai,ai))×inv2∑i=1n∑j=1nlcm(ai,aj)−∑i=1nain=1e6∑i=1n∑j=1ni×jgcd(i,j)×cnti×cntj∑d=1nd∑i=1ndi×cntid∑j=1ndj×cntjd[gcd(i,j)=1]∑d=1nd∑k=1ndk2μ(k)(∑i=1nkdi×cntikd)2T=kd∑T=1nT(∑i=1nTi×cntiT)2∑k∣Tkμ(k)\sum_{i = 0} ^{n - 2} \sum_{j = i + 1} ^{n - 1}lcm(a_i, a_j)\\ 更改下标从1开始\\ \sum_{i = 1} ^{n - 1} \sum_{j = i + 1} ^{n} lcm(a_i, a_j)\\ (\sum_{i = 1} ^{n} \sum_{j = 1} ^{n} lcm(a_i, a_j) - \sum_{i = 1} ^{n} lcm(a_i, a_i)) \times inv2\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} lcm(a_i, a_j) - \sum_{i = 1} ^{n} a_i\\ n = 1e6\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \frac{i \times j}{gcd(i, j)} \times cnt_i \times cnt_j\\ \sum_{d = 1} ^{n} d \sum_{i = 1} ^{\frac{n}{d}}i \times cnt_{id} \sum_{j = 1} ^{\frac{n}{d}} j\times cnt_{jd}[gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} d \sum_{k = 1} ^{\frac{n}{d}} k ^ 2 \mu(k) \left(\sum_{i = 1} ^{\frac{n}{kd}} i \times cnt_{ikd} \right) ^ 2\\ T = kd\\ \sum_{T = 1} ^{n}T \left(\sum_{i = 1} ^{\frac{n}{T}} i \times cnt_{iT} \right) ^ 2 \sum_{k \mid T} k \mu(k) i=0n2j=i+1n1lcm(ai,aj)1i=1n1j=i+1nlcm(ai,aj)(i=1nj=1nlcm(ai,aj)i=1nlcm(ai,ai))×inv2i=1nj=1nlcm(ai,aj)i=1nain=1e6i=1nj=1ngcd(i,j)i×j×cnti×cntjd=1ndi=1dni×cntidj=1dnj×cntjd[gcd(i,j)=1]d=1ndk=1dnk2μ(k)i=1kdni×cntikd2T=kdT=1nTi=1Tni×cntiT2kTkμ(k)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10, mod = 998244353;ll sum[N], c[N], n, m, all;bool st[N];int prime[N], mu[N], cnt;ll quick_pow(ll a, ll n, ll mod) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;n >>= 1;a = a * a % mod;}return ans;
}void init() {mu[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[cnt++] = i;mu[i] = -1;}for(int j = 0; j < cnt && i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) break;mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {sum[j] = (sum[j] + i * mu[i] % mod + mod) % mod;}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();scanf("%lld", &n), m = 0;for(int i = 1; i <= n; i++) {ll x;scanf("%lld", &x);all = (all + x) % mod;m = max(x, m);c[x]++;}ll ans = 0;for(ll t = 1; t <= m; t++) {ll res = 0;for(ll i = 1; i <= m / t; i++) {res = (res + 1ll * i * c[i * t] % mod) % mod;}ans = (ans + t * res % mod * res % mod * sum[t] % mod) % mod;}printf("%lld\n", ((ans - all) * quick_pow(2, mod - 2, mod) % mod + mod) % mod);return 0;
}

Dirichlet k -th root

(f∗g)(n)=∑d∣nf(d)g(nd)g=fk=f∗f∗f⋯∗fgivegsolveffk1k=f=g1k=ginv(k)然后迪利克雷卷积快速幂即可(f * g)(n) = \sum_{d \mid n}f(d) g(\frac{n}{d})\\ g = f ^ k = f * f * f \dots * f\\ give\ g\ solve\ f\\ f ^ {k ^ {\frac{1}{k}}} = f = g ^{\frac{1}{k}} = g ^{inv(k)}\\ 然后迪利克雷卷积快速幂即可\\ (fg)(n)=dnf(d)g(dn)g=fk=ffffgive g solve ffkk1=f=gk1=ginv(k)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10, mod = 998244353;int n, k;ll quick_pow(ll a, int n) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}ll inv(int x) {return quick_pow(x, mod - 2);
}struct matrix {ll a[N];void init() {memset(a, 0, sizeof a);}matrix operator * (const matrix &t) const {matrix ans;ans.init();for(int i = 1; i <= n; i++) {for(int j = i; j <= n; j += i) {ans.a[j] = (ans.a[j] + a[i] * t.a[j / i] % mod) % mod;}}return ans;}
}a, ans;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &k);k = inv(k);for(int i = 1; i <= n; i++) {scanf("%lld", &a.a[i]);}ans.a[1] = 1;while(k) {if(k & 1) ans = ans * a;a = a * a;k >>= 1;}for(int i = 1; i <= n; i++) {printf("%lld%c", ans.a[i], i == n ? '\n' : ' ');}return 0;
}

GCD of Divisors

f(n)=∑d∣ngcd(d,nd)∑i=1n∑d∣igcd(d,id)∑d=1n∑d∣igcd(d,id)∑d=1n∑i=1ndgcd(d,i)∑d=1n∑i=1nd∑k∣gcd(d,i)ϕ(k)∑d=1n∑k∣dϕ(k)nkd∑k=1nϕ(k)∑d=1nknk2d设f(n)=∑i=1nni原式子=∑k=1k×k<=nϕ(k)f(nk2)f(n) = \sum_{d \mid n} gcd(d, \frac{n}{d})\\ \sum_{i = 1} ^{n} \sum_{d \mid i} gcd(d, \frac{i}{d})\\ \sum_{d = 1} ^{n} \sum_{d \mid i} gcd(d, \frac{i}{d})\\ \sum_{d = 1} ^{n} \sum_{i = 1} ^{\frac{n}{d}} gcd(d, i)\\ \sum_{d = 1} ^{n} \sum_{i = 1} ^{\frac{n}{d}} \sum_{k \mid gcd(d, i)} \phi(k)\\ \sum_{d = 1} ^{n} \sum_{k \mid d} \phi(k) \frac{n}{kd}\\ \sum_{k = 1} ^{n} \phi(k) \sum_{d = 1} ^{\frac{n}{k}} \frac{n}{k ^ 2 d}\\ 设f(n) = \sum_{i = 1} ^{n} \frac{n}{i}\\ 原式子 = \sum_{k = 1} ^{k \times k <= n} \phi(k) f(\frac{n}{k ^ 2})\\ f(n)=dngcd(d,dn)i=1ndigcd(d,di)d=1ndigcd(d,di)d=1ni=1dngcd(d,i)d=1ni=1dnkgcd(d,i)ϕ(k)d=1nkdϕ(k)kdnk=1nϕ(k)d=1knk2dnf(n)=i=1nin=k=1k×k<=nϕ(k)f(k2n)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 4e7 + 10;ll prime[N], phi[N], cnt;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);}}
}ll calc(ll n) {ll ans = 0;for(ll l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans += (r - l + 1) * (n / l);}return 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();ll n, ans = 0;scanf("%lld", &n);for(int i = 1; 1ll * i * i <= n; i++) {ans += phi[i] * calc(n / i / i);}cout << ans << "\n";return 0;
}

Absolute Math

定义c(n)=theprimenhave,c(1)=0,c(2)=1,c(4)=1,c(6)=2f(n)=∑d∣nμ(d)2=2c(n)f(ab)=f(a)×f(b)f(gcd(a,b))∑i=1mf(n)×f(i)f(gcd(n,i))f(n)∑d∣n1f(d)∑i=1mdf(id)[gcd(i,nd)=1]f(n)∑d∣n1f(d)∑k∣ndμ(k)∑i=1mkdf(ikd)T=kdf(n)∑T∣n∑i=1mTf(iT)∑d∣Tμ(Td)f(d)F(m,n)=∑i=1mf(in),g(n)=∑d∣nμ(nd)f(d)F(m,n)=f(n)∑T∣nF(mT,T)g(T)g=μ∗1f,是一个积性函数g(1)=1g(p)=μ(p)f(1)+μ(1)f(p)=−1+12=−12g(px)[x>=2]=μ(1)f(px)+μ(p)f(px−1)=12−12=0然后递归求解,卡卡常定义c(n) = the\ prime\ n\ have,c(1) = 0, c(2) = 1, c(4) = 1, c(6) = 2 \\ f(n) = \sum_{d \mid n} \mu(d) ^ 2 = 2 ^{c(n)} \\ f(ab) = \frac{f(a) \times f(b)}{f(gcd(a, b))}\\ \sum_{i = 1} ^{m} \frac{f(n) \times f(i)}{f(gcd(n, i))}\\ f(n) \sum_{d \mid n} \frac{1}{f(d)} \sum_{i = 1} ^{\frac{m}{d}} f(id)[gcd(i, \frac{n}{d}) = 1]\\ f(n) \sum_{d \mid n} \frac{1}{f(d)} \sum_{k \mid \frac{n}{d}} \mu(k) \sum_{i = 1} ^{\frac{m}{kd}} f(ikd)\\ T = kd\\ f(n) \sum_{T \mid n} \sum_{i = 1} ^{\frac{m}{T}}f(iT) \sum_{d \mid T} \frac{\mu(\frac{T}{d})}{f(d)}\\ F(m, n) = \sum_{i = 1} ^{m} f(in), g(n) = \sum_{d \mid n} \frac{\mu(\frac{n}{d})}{f(d)}\\ F(m, n) = f(n) \sum_{T \mid n} F(\frac{m}{T}, T) g(T)\\ g = \mu * \frac{1}{f},是一个积性函数\\ g(1) = 1\\ g(p) = \frac{\mu(p)}{f(1)} + \frac{\mu(1)}{f(p)} = -1 + \frac{1}{2} = -\frac{1}{2}\\ g(p ^ x)[x >= 2] = \frac{\mu(1)}{f(p ^ x)} + \frac{\mu(p)}{f(p ^{x - 1})} = \frac{1}{2} - \frac{1}{2} = 0\\ 然后递归求解,卡卡常\\ c(n)=the prime n havec(1)=0,c(2)=1,c(4)=1,c(6)=2f(n)=dnμ(d)2=2c(n)f(ab)=f(gcd(a,b))f(a)×f(b)i=1mf(gcd(n,i))f(n)×f(i)f(n)dnf(d)1i=1dmf(id)[gcd(i,dn)=1]f(n)dnf(d)1kdnμ(k)i=1kdmf(ikd)T=kdf(n)Tni=1Tmf(iT)dTf(d)μ(dT)F(m,n)=i=1mf(in),g(n)=dnf(d)μ(dn)F(m,n)=f(n)TnF(Tm,T)g(T)g=μf1g(1)=1g(p)=f(1)μ(p)+f(p)μ(1)=1+21=21g(px)[x>=2]=f(px)μ(1)+f(px1)μ(p)=2121=0

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e7 + 10, mod = 1e9 + 7, inv2 = mod + 1 >> 1;int prime[N], sum[N], g[N], f[N], cnt;bool st[N];void init() {f[1] = g[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;f[i] = 2;g[i] = mod - inv2;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {f[i * prime[j]] = f[i];break;}f[i * prime[j]] = f[i] * 2;g[i * prime[j]] = 1ll * g[i] * g[prime[j]] % mod;}}for(int i = 1; i < N; i++) {sum[i] = (f[i] + sum[i - 1]) % mod;}
}ll solve(int m, int n) {if(m == 0) return 0;if(m == 1) return f[n];if(n == 1) return sum[m];ll ans = 0;//发现m <= 20的时候是较优的……if(m <= 20 && 1ll * n * m < N) {for(int i=1;i<=m;i++)ans = (ans + f[i * n]) % mod;return ans;}for(int i = 1; 1ll * i * i <= n; i++) {if(n % i == 0){if(g[i]){ans = (ans + solve(m / i, i) * g[i] % mod) % mod;}if(i * i !=n && g[n / i]){ans = (ans + solve(m / (n / i), n / i)*g[n / i]) % mod;}}}return ans * f[n] % mod;
}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;scanf("%d", &T);while(T--) {int n, m;scanf("%d %d", &n, &m);printf("%lld\n", solve(m, n));}return 0;
}
/*杭电的测评机比自己的老年机跑的还慢……
*/

Counting Divisors

∑i=1nd(ik)caculte:∑i=lrd(ik)d(n)是一个积性函数d(1)=1d(p)=2d(pk)=k+1然后用区间质数筛就可以得到答案了。\sum_{i = 1} ^{n} d(i ^ k)\\ caculte:\sum_{i = l} ^{r} d(i ^ k)\\ d(n)是一个积性函数\\ d(1) = 1\\ d(p) = 2\\ d(p ^ k) = k + 1\\ 然后用区间质数筛就可以得到答案了。 i=1nd(ik)caculte:i=lrd(ik)d(n)d(1)=1d(p)=2d(pk)=k+1

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10, mod = 998244353;int prime[N], cnt;ll a[N], num[N], l, r, k;bool st[N];void init() {for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}}}
}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;scanf("%d", &T);while(T--) {scanf("%lld %lld %lld", &l, &r, &k);for(int i = 0; i <= r - l; i++) a[i] = l + i, num[i] = 1;for(int i = 1; 1ll * prime[i] * prime[i] <= r; i++) {ll st = l / prime[i] * prime[i];if(st < l) st += prime[i];for(ll j = st; j <= r; j += prime[i]) {ll res = 0;while(a[j - l] % prime[i] == 0) {res++;a[j - l] /= prime[i];}num[j - l] = num[j - l] * (res * k % mod + 1) % mod;}}for(int i = 0; i <= r - l; i++) {if(a[i] != 1) {num[i] = num[i] * (k + 1) % mod;}}ll ans = 0;for(int i = 0; i <= r - l; i++) {ans = (ans + num[i]) % mod;}printf("%lld\n", ans);}return 0;
}

Master of Phi

f(n)=∑d∣nϕ(d)ndn=∏i=1npiaif(n)=(ϕ∗id)(n)f=ϕ∗idf∗I=ϕ∗id∗I=id∗id=id×σ(id)f=(id×σ(id))∗μ=∑d∣nμ(d)×nd×σ(nd)f(pk)=pk−1×(pk+p−k)因为f(n)是一个积性函数,所以只要算出每一个质数项然后直接相乘即可f(n) = \sum_{d \mid n} \phi(d) \frac{n}{d}\\ n = \prod_{i = 1} ^{n} p_i ^{a_i}\\ f(n) = (\phi * id)(n)\\ f = \phi * id\\ f * I = \phi * id * I = id * id = id \times \sigma(id)\\ f = (id \times \sigma(id) ) * \mu = \sum_{d \mid n} \mu(d) \times \frac{n}{d} \times \sigma(\frac{n}{d})\\ f(p ^ k) = p ^{k - 1}\times(pk + p - k)\\ 因为f(n)是一个积性函数,所以只要算出每一个质数项然后直接相乘即可\\ f(n)=dnϕ(d)dnn=i=1npiaif(n)=(ϕid)(n)f=ϕidfI=ϕidI=idid=id×σ(id)f=(id×σ(id))μ=dnμ(d)×dn×σ(dn)f(pk)=pk1×(pk+pk)f(n)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int mod = 998244353;ll quick_pow(ll a, int n) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return 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, m;scanf("%d", &T);while(T--) {ll ans = 1, p, k;scanf("%d", &m);for(int i = 1; i <= m; i++) {scanf("%lld %lld", &p, &k);ans = (p * k % mod + p - k + mod) % mod * ans % mod * quick_pow(p, k - 1) % mod;}printf("%lld\n", ans);}return 0;
}

Just a Math Problem

f(k)equalthenumberofprimefactorsinkg(k)=2f(k)=∑d∣kμ(d)2∑i=1ng(i)∑i=1n∑d∣iμ(d)2μ(d)2=∑k2∣dμ(k)∑i=1n∑d∣i∑k2∣dμ(k)∑k=1k×k≤nμ(k)∑k2∣d∑d∣i∑k=1k×k≤nμ(k)∑a=1nk2∑b=1nak2∑k=1k×k≤nμ(k)∑i=1nk2nik2f(k)\ equal\ the\ number\ of\ prime\ factors\ in\ k\\ g(k) = 2 ^{f(k)} = \sum_{d \mid k} \mu(d) ^ 2 \\ \sum_{i = 1} ^{n} g(i)\\ \sum_{i = 1} ^{n} \sum_{d \mid i} \mu(d) ^ 2\\ \mu(d) ^ 2 = \sum_{k ^ 2 \mid d} \mu(k)\\ \sum_{i = 1} ^{n} \sum_{d \mid i} \sum_{k ^ 2 \mid d} \mu(k)\\ \sum_{k = 1} ^{k \times k \leq n} \mu(k) \sum_{k ^ 2 \mid d} \sum_{d \mid i}\\ \sum_{k = 1} ^{k \times k \leq n} \mu(k) \sum_{a = 1} ^{\frac{n}{k ^ 2}} \sum_{b = 1} ^{\frac{n}{a k ^ 2}}\\ \sum_{k = 1} ^{k \times k \leq n} \mu(k) \sum_{i = 1} ^{\frac{n}{k ^ 2}} \frac{n}{i k ^ 2}\\ f(k) equal the number of prime factors in kg(k)=2f(k)=dkμ(d)2i=1ng(i)i=1ndiμ(d)2μ(d)2=k2dμ(k)i=1ndik2dμ(k)k=1k×knμ(k)k2ddik=1k×knμ(k)a=1k2nb=1ak2nk=1k×knμ(k)i=1k2nik2n

#include<cstdio>
typedef long long ll;
const int N=1000010,P=1000000007;
int T,C,tot,p[N/10],i,j,ans,f[N];char mu[N],v[N];ll n;
inline int F(ll n){if(n<N)if(f[n])return f[n];ll t=0;for(ll i=1,j;i<=n;i=j+1)j=n/(n/i),t+=n/i*(j-i+1);t%=P;if(n<N)f[n]=t;return t;
}
int main(){for(mu[1]=1,i=2;i<N;i++){if(!v[i])mu[i]=-1,p[tot++]=i;for(j=0;j<tot&&i*p[j]<N;j++){v[i*p[j]]=1;if(i%p[j])mu[i*p[j]]=-mu[i];else break;}}for(scanf("%d",&T);T--;printf("Case #%d: %d\n",++C,(ans+P)%P)){scanf("%I64d",&n);for(ans=0,i=1;i<=n/i;i++)if(mu[i])ans=(ans+F(n/i/i)*mu[i])%P;}return 0;
}

D. Winter is here

f(n)=∑k[gcd(ai1,ai2,ai3…aik−1,ak)=n]ans=∑i=2nif(i)F(n)=∑k[n∣ai1,n∣ai2,n∣ai3…n∣aik−1,n∣aik]cntn表示是n的倍数的数字的个数=∑i=1cntniCcntni=cntn×2cntn−1F(d)=∑d∣nf(n)f(d)=∑d∣nF(n)μ(nd)f(n) = \sum k[gcd(a_{i1}, a_{i2}, a_{i3} \dots a_{i_{k - 1}}, a_{k}) = n]\\ ans = \sum_{i = 2} ^{n} i f(i)\\ F(n) = \sum k[n \mid a_{i1}, n \mid a_{i2}, n \mid _{ai3} \dots n \mid a_{i_{k - 1}}, n \mid a_{i_k}]\\ cnt_n表示是n的倍数的数字的个数\\ = \sum_{i = 1} ^{cnt_n} i C_{cnt_n} ^{i} = cnt_n \times 2 ^{cnt_n - 1}\\ F(d) = \sum_{d \mid n} f(n)\\ f(d) = \sum_{d \mid n} F(n) \mu(\frac{n}{d})\\ f(n)=k[gcd(ai1,ai2,ai3aik1,ak)=n]ans=i=2nif(i)F(n)=k[nai1,nai2,nai3naik1,naik]cntnn=i=1cntniCcntni=cntn×2cntn1F(d)=dnf(n)f(d)=dnF(n)μ(dn)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10, mod = 1e9 + 7;int prime[N], mu[N], num[N], f[N], cnt, n;vector<int> fac[N];bool st[N];ll quick_pow(ll a, int n) {ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}void init() {mu[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {prime[++cnt] = i;mu[i] = -1;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {break;}mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) {for(int j = 2 * i; j < N; j += i) {num[i] += num[j];}if(num[i]) {num[i] = 1ll * num[i] * quick_pow(2, num[i] - 1) % mod;}}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {f[i] = (f[i] + 1ll * num[j] * mu[j / i] % mod + mod) % mod;}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> n;for(int i = 1; i <= n; i++) {int x;cin >> x;num[x]++;}init();ll ans = 0;for(int i = 2; i < N; i++) {ans = (ans + 1ll * i * f[i] % mod) % mod;}cout << ans << "\n";return 0;
}

Battlestation Operational

∑i=1n∑j=1i⌈ij⌉[gcd(i,j)=1]∑i=1n∑j=1i(ij+1)[gcd(i,j)=1]−∑i=1n∑j=1i[gcd(i,j)=1,j∣i]∑i=1n∑j=1i(ij+1)[gcd(i,j)=1]−n∑i=1n∑j=1iij[gcd(i,j)=1]+∑i=1n∑j=1i[gcd(i,j)=1]−n∑i=1n∑j=1iij[gcd(i,j)=1]+∑i=1ϕ(i)−n∑i=1n∑j=1iij[gcd(i,j)=1]∑d=1nμ(d)∑i=1nd∑j=1iij求解∑i=1n∑j=1iij=∑i=1n∑j=inji可以发现这个东西具有区间分块性质所以可以利用差分然后求前缀和得到,具体实现看代码整体复杂度O(nlog⁡n+T×n)\sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \lceil \frac{i}{j} \rceil [gcd(i, j) = 1]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i}( \frac{i}{j} + 1)[gcd(i, j) = 1] - \sum_{i = 1} ^{n}\sum_{j = 1} ^{i}[gcd(i, j) = 1, j \mid i]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i}(\frac{i}{j} + 1)[gcd(i, j) = 1] - n\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j}[gcd(i, j) = 1] + \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} [gcd(i, j) = 1] - n\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j}[gcd(i, j) = 1] + \sum_{i = 1} \phi(i) - n\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j}[gcd(i, j)= 1]\\ \sum_{d = 1} ^{n} \mu(d) \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{i} \frac{i}{j}\\ 求解\sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \frac{i}{j} = \sum_{i = 1} ^{n} \sum_{j = i} ^{n} \frac{j}{i}\\ 可以发现这个东西具有区间分块性质\\所以可以利用差分然后求前缀和得到,具体实现看代码\\ 整体复杂度O(n \log n + T \times \sqrt n)\\ i=1nj=1iji[gcd(i,j)=1]i=1nj=1i(ji+1)[gcd(i,j)=1]i=1nj=1i[gcd(i,j)=1,ji]i=1nj=1i(ji+1)[gcd(i,j)=1]ni=1nj=1iji[gcd(i,j)=1]+i=1nj=1i[gcd(i,j)=1]ni=1nj=1iji[gcd(i,j)=1]+i=1ϕ(i)ni=1nj=1iji[gcd(i,j)=1]d=1nμ(d)i=1dnj=1ijii=1nj=1iji=i=1nj=inij西O(nlogn+T×n)

/*Author : lifehappy
*/	
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10, mod = 1e9 + 7;int prime[N], phi[N], mu[N], f[N], cnt, n;bool st[N];void init() {mu[1] = phi[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 = 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);mu[i * prime[j]] = -mu[i];}}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {int l = j, r = j + i;f[l] = (f[l] + (j / i)) % mod;if(r < N) f[r] = (f[r] - (j / i) + mod) % mod;}}for(int i = 1; i < N; i++) {phi[i] = (phi[i] + phi[i - 1]) % mod;mu[i] = (mu[i] + mu[i - 1] + mod) % mod;f[i] = (f[i] + f[i - 1]) % mod;}for(int i = 1; i < N; i++) {f[i] = (f[i] + f[i - 1]) % mod;}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();while(scanf("%d", &n) != EOF) {ll ans = 0;for(int l = 1, r; l <= n; l = r + 1) {r = n / (n / l);ans = (ans + 1ll * (mu[r] - mu[l - 1]) * f[n / l] % mod + mod) % mod;}ans = (ans + phi[n] - n + mod) % mod;printf("%lld\n", ans);}return 0;
}

RXD and math

∑i=1nkμ2(i)×nki\sum_{i = 1} ^{n ^ k} \mu ^ 2(i) \times \sqrt{\frac{n ^ k}{i}}\\ i=1nkμ2(i)×ink

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int mod = 1e9 + 7;int quick_pow(ll a, ll n) {a %= mod;ll ans = 1;while(n) {if(n & 1) ans = ans * a % mod;a = a * a % mod;n >>= 1;}return ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);ll n, k, cas = 1;while(cin >> n >> k) {printf("Case #%d: %d\n", cas++, quick_pow(n, k));}return 0;
}

GCD?LCM!

F(n)=∑i=1n∑j=1n[lcm(i,j)+gcd(i,j)≥n]F(n)=n2−∑i=1n∑j=1n[lcm(i,j)+gcd(i,j)<n)]F(n)−F(n−1)=n2−(n−1)2−∑i=1n∑j=1n[lcm(i,j)+gcd(i,j)<n]+∑i=1n−1∑j=1n−1[lcm(i,j)+gcd(i,j)<n−1]F(n)−F(n−1)=2×n−1−∑i=1n−1∑j=1n−1[lcm(i,j)+gcd(i,j)=n−1]g(n)=∑i=1n∑j=1n[lcm(i,j)+gcd(i,j)=n]g(n)=∑d∣n∑i=1nd∑j=1nd[ijd+d=n][gcd(i,j)=1]=∑d∣n∑i=1nd∑j=1nd[ij=nd−1][gcd(i,j=1)]∑d∣n∑i=1nd−1∑j=1nd−1[ij=nd−1][gcd(i,j)=1]k(n)=∑i=1n∑j=1n[ij=n][gcd(i,j)=1]k(1)=1,k(p)=2,k(pk)=2,且为积性函数所以可以质数筛得到然后再通过一次埃筛得到g(n),即可O(n)得到F(n)F(n) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} [lcm(i, j) + gcd(i, j) \geq n]\\ F(n) = n ^ 2 - \sum_{i = 1} ^{n} \sum_{j = 1} ^{n}[lcm(i, j) + gcd(i, j) < n)]\\ F(n) - F(n - 1) = n ^ 2 - (n - 1) ^ 2 - \sum_{i = 1} ^{n} \sum_{j = 1} ^{n}[lcm(i, j) + gcd(i, j) < n] + \sum_{i = 1} ^{n - 1} \sum_{j = 1} ^{n - 1}[lcm(i, j) + gcd(i, j) < n - 1]\\ F(n) - F(n - 1) = 2 \times n - 1 - \sum_{i = 1} ^{n - 1} \sum_{j = 1} ^{n - 1} [lcm(i, j) + gcd(i, j) = n - 1]\\ g(n) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} [lcm(i, j) + gcd(i, j) = n]\\ g(n) = \sum_{d \mid n} \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}[ijd + d = n ][gcd(i, j) = 1]\\ = \sum_{d \mid n} \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}}[ij = \frac{n}{d} - 1][gcd(i, j = 1)]\\ \sum_{d \mid n} \sum_{i = 1} ^{\frac{n}{d} - 1} \sum_{j = 1} ^{\frac{n}{d} - 1}[ij = \frac{n}{d} - 1][gcd(i, j) = 1]\\ k(n) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{n}[ij = n][gcd(i, j) = 1]\\ k(1) = 1, k(p) = 2,k (p ^ k) = 2,且为积性函数所以可以质数筛得到\\ 然后再通过一次埃筛得到g(n),即可O(n)得到F(n)\\ F(n)=i=1nj=1n[lcm(i,j)+gcd(i,j)n]F(n)=n2i=1nj=1n[lcm(i,j)+gcd(i,j)<n)]F(n)F(n1)=n2(n1)2i=1nj=1n[lcm(i,j)+gcd(i,j)<n]+i=1n1j=1n1[lcm(i,j)+gcd(i,j)<n1]F(n)F(n1)=2×n1i=1n1j=1n1[lcm(i,j)+gcd(i,j)=n1]g(n)=i=1nj=1n[lcm(i,j)+gcd(i,j)=n]g(n)=dni=1dnj=1dn[ijd+d=n][gcd(i,j)=1]=dni=1dnj=1dn[ij=dn1][gcd(i,j=1)]dni=1dn1j=1dn1[ij=dn1][gcd(i,j)=1]k(n)=i=1nj=1n[ij=n][gcd(i,j)=1]k(1)=1,k(p)=2,k(pk)=2g(n)O(n)F(n)

/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e6 + 10, mod = 258280327;ll prime[N], k[N], primes[N], g[N], f[N], cnt, n;bool st[N];void init() {k[1] = 1;for(int i = 2; i < N; i++) {if(!st[i]) {primes[i] = i;prime[++cnt] = i;k[i] = 2;}for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if(i % prime[j] == 0) {k[i * prime[j]] = k[i / primes[i]] * 2;primes[i * prime[j]] = primes[i] * prime[j];break;}k[i * prime[j]] = k[i] * 2;primes[i * prime[j]] = prime[j];}}for(int i = 1; i < N; i++) {for(int j = i; j < N; j += i) {g[j] = (g[j] + k[j / i - 1]) % mod;}}for(int i = 1; i < N; i++) {f[i] = (f[i - 1] + 2ll * i - 1 - g[i - 1] + mod) % mod;}for(int i = 1; i < N; i++) {f[i] = (f[i - 1] + f[i]) % mod;}
}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;scanf("%d", &T);while(T--) {scanf("%d", &n);printf("%lld\n", f[n]);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/314056.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于SQLite+EF6实现一套自己的Key-Value存储管理工具包(3)

前面两边的代码就是一套初步的工具包架构&#xff0c;基本底层通用&#xff0c;可以移植到任意项目实现类似的需求。接下来&#xff0c;再在我们特定的项目几微助手里面再实现一套基于自己项目的基类&#xff0c;根据项目需求抽象一下项目内的常用方法。理论上&#xff0c;这一…

2019-03-09-算法-进化(从排序数组中删除重复项)

题目描述 给定一个排序数组&#xff0c;你需要在原地删除重复出现的元素&#xff0c;使得每个元素只出现一次&#xff0c;返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 示例 1 给定数组 nums…

P6329 【模板】点分树 | 震波

P6329 【模板】点分树 | 震波 这是一道模板题&#xff0c;需要支持两个操作&#xff0c;操作一就是单点修改点权&#xff0c;操作二就是查询距离x不超过k的点权值和。 我们考虑建出点分树&#xff0c;然后对于每个点维护两个数据结构&#xff0c;一个处理当前分治范围到当前点…

基于SQLite+EF6实现一套自己的Key-Value存储管理工具包(1)

在项目中&#xff0c;经常会需要对一些特定的业务对象进行属性的扩展&#xff0c;而且这些属性的扩展还具备极不可预测性、相互关系松散等特点。大部分的开发人员是最讨厌这类涉及到数据字段扩展的需求变更。这种调整&#xff0c;轻则数据要加字段&#xff0c;重则程序代码要做…

2019-03-09-算法-进化(买卖股票的最佳时机 II)

题目描述 给定一个数组&#xff0c;它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易&#xff08;多次买卖一支股票&#xff09;。 注意&#xff1a;你不能同时参与多笔交易&#xff08;你必须在再次购买…

【C】Natasha V1.3.6.0 的升级日志

文章转载授权级别&#xff1a;C 预计阅读时间&#xff1a;8分钟开源库满足于个人&#xff0c;而完善于大众。Natasha 自稳定版发布之后&#xff0c;众多老铁参与增强改进&#xff0c;感谢如下老铁的反馈&#xff1a;1. 异常搜集在 wenjq0911 建议下&#xff0c;添加…

牛客练习赛69 解方程

解方程 ∑d∣nf(d)σp(nd)σq(n)f∗σpσq有σk∑d∣ndkidk∗If∗idp∗Iidq∗I∑d∣nμ(d)μ∗I对上面式子同时卷上一个μf∗idpidq因为idk是一个完全积性函数&#xff0c;所以idp−1μidpidk∗(μidp)∑d∣ndkμ(nd)(nd)k∑d∣nμ(d)ϵfidq∗(μidp)f(n)∑d∣nμ(d)dp(nd)qf(1…

2019-03-09-算法-进化(旋转数组)

题目描述 给定一个数组&#xff0c;将数组中的元素向右移动 k 个位置&#xff0c;其中 k 是非负数。 示例 1: 输入: [1,2,3,4,5,6,7] 和 k 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,5] 向右旋转 3 步: [5,6,7,1,2,3,4]示…

.NET 程序员如何学习Vue

之所以取这个标题&#xff0c;是因为本文来自内部培训的整理&#xff0c;培训的对象是公司的 .NET 程序员&#xff0c;.NET 程序员学习 Vue 是为了在项目中做二次开发时能够更好地跟产品对接。Vue 是现在比较流行的前端框架&#xff0c;也是非常容易入门的前端框架&#xff0c;…

P3714 [BJOI2017]树的难题(点分治/线段树/单调队列)

P3714 [BJOI2017]树的难题 求解树上长度在L到R的树链中颜色段权值和最大的链。 首先求解树上链的问题&#xff0c;而且限制了链的长度&#xff0c;那么我们需要点分治处理&#xff0c;然后考虑每次分治&#xff0c;我们可以把链分成两类&#xff0c;先处理同色连通块&#xf…

A Simple Math Problem(2020 ICPC 江西省省赛)

A Simple Math Problem ∑i1n∑j1if(j)[gcd(i,j)1]∑i1n∑jinf(i)[gcd(i,j)1]∑i1n∑j1nf(i)[gcd(i,j)1]−∑i1n∑j1if(i)[gcd(i,j)1]f(1)∑d1nμ(d)nd∑i1ndf(id)−∑i1nf(i)ϕ(i)f(1)然后就可以O(nlog⁡n)求解了\sum_{i 1} ^{n} \sum_{j 1} ^{i} f(j)[gcd(i, j) 1]\\ \sum…

2019-03-10-算法-进化(存在重复)

给定一个整数数组&#xff0c;判断是否存在重复元素。 如果任何值在数组中出现至少两次&#xff0c;函数返回 true。如果数组中每个元素都不相同&#xff0c;则返回 false。 示例 1: 输入: [1,2,3,1] 输出: true示例 2: 输入: [1,2,3,4] 输出: false示例 3: 输入: [1,1,1,…

P3233 [HNOI2014]世界树(虚树/倍增/动态规划)

P3233 [HNOI2014]世界树 每次给出mi个点&#xff0c;查询每个点控制的点的个数&#xff0c;每个点由离他最近编号最小的点控制。 首先按照套路建出虚树&#xff0c;注意特判1节点&#xff0c;然后用栈维护右链&#xff0c;然后不断弹栈建边。 然后进行dp&#xff0c;求解出虚…

.Net之微信小程序获取用户UnionID

前言&#xff1a;在实际项目开发中我们经常会遇到账号统一的问题&#xff0c;如何在不同端或者是不同的登录方式下保证同一个会员或者用户账号唯一&#xff08;便于用户信息的管理&#xff09;。这段时间就有一个这样的需求&#xff0c;之前有个客户做了一个微信小程序商城&…

Absolute Math (HDU 6868)

Absolute Math 定义c(n)theprimenhave&#xff0c;c(1)0,c(2)1,c(4)1,c(6)2f(n)∑d∣nμ(d)22c(n)f(ab)f(a)f(b)f(gcd(a,b))∑i1mf(n)f(i)f(gcd(n,i))f(n)∑d∣n1f(d)∑i1mdf(id)[gcd(i,nd)1]f(n)∑d∣n1f(d)∑k∣ndμ(k)∑i1mkdf(ikd)Tkdf(n)∑T∣n∑i1mTf(iT)∑d∣Tμ(Td)f(d…

2019-03-10-算法-进化(只出现一次的数字)

题目描述 给定一个非空整数数组&#xff0c;除了某个元素只出现一次以外&#xff0c;其余每个元素均出现两次。找出那个只出现了一次的元素。 说明&#xff1a; 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗&#xff1f; 示例 1: 输入: [2,2,1] 输出: 1…

自由源自于自律 及其他三则分享

Office 365 官方公众号的新创深度内容推荐竹板这么一打呀&#xff0c;别的咱不夸&#xff0c;单说我们的Office 365官方公众号&#xff08;“微软Office365”&#xff09;&#xff0c;近一段时间来在内容创作上面有一些新的突破——推出了一个关于探讨大脑及思维运作的专题。请…

CF871E Restore the Tree(构造/哈希)

CF871E Restore the Tree 对于一个n个点的树&#xff0c;给定k个点以及这k个点到n个点的距离&#xff0c;然后构造出一颗合法的树。如果不存在合法的树输出-1&#xff0c;任意输出一组解即可。 首先处理在关键点链上的节点&#xff0c;对于两个关键点&#xff0c;有且仅有他们…

Battlestation Operational HDU 6134

Battlestation Operational ∑i1n∑j1i⌈ij⌉[gcd(i,j)1]∑i1n∑j1i(ij1)[gcd(i,j)1]−∑i1n∑j1i[gcd(i,j)1,j∣i]∑i1n∑j1i(ij1)[gcd(i,j)1]−n∑i1n∑j1iij[gcd(i,j)1]∑i1n∑j1i[gcd(i,j)1]−n∑i1n∑j1iij[gcd(i,j)1]∑i1ϕ(i)−n∑i1n∑j1iij[gcd(i,j)1]∑d1nμ(d)∑i1n…

2019-03-11-算法-进化(求众数)

题目描述 给定一个大小为 n 的数组&#xff0c;找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的&#xff0c;并且给定的数组总是存在众数。 示例 1: 输入: [3,2,3] 输出: 3示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2思路&#xff1a…