8.7

· · 个人记录

AT_abc020_D

直接拆开 lcm 然后推式子:

\begin{aligned} \sum_{i=1}^Nlcm(i,K) &= \sum_{i=1}^N\frac{iK}{gcd(i,K)}\\ &= \sum_{d|K}\sum_{i=1}^{\lfloor \frac{N}{d} \rfloor} iK[gcd(i,\frac{K}{d})=1]\\ &= K\sum_{d|K}\sum_{x|\frac{K}{d}} \mu(x)xS(\lfloor \frac{N}{dx} \rfloor) \end{aligned}

直接枚举 d,x,暴力查询单个 \mu 并记忆化即可。

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int mod = 1e9 + 7;

int n, k;
unordered_map<int, int> mp;

int calc(int x) {
    return (x + 1) * x / 2 % mod;
}

int mu(int x) {
    if (mp.count(x))
        return mp[x];
    int ans = 1, tmp = x;
    for (int i = 2; i * i <= x; i++)
        if (x % i == 0) {
            int cnt = 0;
            while (x % i == 0)
                x /= i, cnt++;
            if (cnt > 1)
                return 0;
            ans = -ans;
        }
    if (x > 1)
        ans = -ans;
    mp[tmp] = ans;
    return ans;
}

int S(int x, int y) {
    int ans = 0;
    for (int i = 1; i * i <= y; i++)
        if (y % i == 0) {
            ans = (ans + mu(i) * i % mod * calc(x / i) % mod + mod) % mod;
            if (i * i != y)
                ans = (ans + mu(y / i) * (y / i) % mod * calc(x / (y / i)) % mod + mod) % mod;
        }
    return ans;
}

signed main() {
    cin >> n >> k;
    int ans = 0;
    for (int i = 1; i * i <= k; i++)
        if (k % i == 0) {
            ans = (ans + S(n / i, k / i)) % mod;
            if (i * i != k)
                ans = (ans + S(n / (k / i), i)) % mod;
        }
    cout << ans * k % mod;
    return 0;
} 

P2231

首先转化一下题意,可以理解为从 [1,m] 中选 n 个正整数,可以重复,使得这些数的 gcd 为 1,求序列数。

可以得到不定方程

\sum_{i = 1}^{n} a_i x_i = 1

那么根据裴蜀定理,只有当 gcd(a_1,a_2,\dots,a_n,m)=1 时有解。

那么答案即为

\sum_{a_1=1}^m\dots\sum_{a_n=1}^m[gcd(a_1,\dots,a_n,m)=1] = \sum_{d|m}\mu(d)\left(\sum_{d|a_1}1\right)\dots\left(\sum_{d|a_n}1\right) = \sum_{d|m}\mu(d)\left(\frac{m}{d}\right)^n
#include <bits/stdc++.h>

#define int long long

using namespace std;

int n, m;
unordered_map<int, int> mp;

int mu(int x) {
    if (mp.count(x))
        return mp[x];
    int ans = 1, tmp = x;
    for (int i = 2; i * i <= x; i++)
        if (x % i == 0) {
            int cnt = 0;
            while (x % i == 0)
                x /= i, cnt++;
            if (cnt > 1)
                return 0;
            ans = -ans;
        }
    if (x > 1)
        ans = -ans;
    mp[tmp] = ans;
    return ans;
}

int fpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

signed main() {
    cin >> n >> m;
    int ans = 0;
    for (int i = 1; i * i <= m; i++)
        if (m % i == 0) {
            ans += mu(i) * fpow(m / i, n);
            if (i * i != m)
                ans += mu(m / i) * fpow(i, n);
        }
    cout << ans;
    return 0;
} 

CF235E

根据P3227,我们可以扩展得到

d(ijk) = \sum_{u|i}\sum_{v|j}\sum_{w|k}[gcd(u,v)=1][gcd(v,w)=1][gcd(w,u)=1]

所以,将其代入原式,并同时使用莫比乌斯反演得到

\sum_{x=1}^a\mu(x)\sum_{y=1}^b\mu(y)\sum_{z=1}^c\mu(z)\left(\sum_{x|u,z|u}^a\lfloor\frac{a}{u}\rfloor\right)\left(\sum_{x|v,y|v}^b\lfloor\frac{b}{v}\rfloor\right)\left(\sum_{y|u,z|u}^c\lfloor\frac{c}{w}\rfloor\right)

注意到 x|u 且 z|u 等价于 lcm(x,z)|u,所以我们定义 S(n,m) = \sum\limits_{m|k}^n\lfloor\frac{n}{k}\rfloor = \sum\limits_{t=1}^{\lfloor n/m \rfloor}\lfloor \frac{n}{mt} \rfloor

则原式变为

\sum_{x=1}^a\mu(x)\sum_{y=1}^b\mu(y)\sum_{z=1}^c\mu(z)S(a,lcm(x,z))S(b,lcm(x,y))S(c,lcm(y,z))

预处理 S(n,m) 数组与任意两数的 lcm 即可。

P10584

首先,任意正整数 x 都可以唯一表示为 x=dy^2 的形式,其中 d 无平方因子,即 \mu^2(d)=1。

则 ij=d_1d_2(xy)^2,那么 ij 是完全平方数当且仅当 d_1=d_2。

所以我们枚举 i,j 的 d,那么

所以答案为

\sum_{d=1}^{min(n,m)}\mu^2(d) \lfloor \sqrt{\frac{n}{d}} \rfloor \lfloor \sqrt{\frac{m}{d}} \rfloor

恒等式

\mu^2(d) = \sum_{k^2|d}\mu(k)

所以,答案又变为

\begin{align*} \sum_{d=1}^{\min(n,m)} \left( \sum_{k^2|d} \mu(k) \right) \left\lfloor \sqrt{\frac{n}{d}} \right\rfloor \left\lfloor \sqrt{\frac{m}{d}} \right\rfloor &= \sum_{k=1}^{\min(n,m)} \mu(k) \left\lfloor \sqrt{\frac{n}{k^2 t}} \right\rfloor \left\lfloor \sqrt{\frac{m}{k^2 t}} \right\rfloor \end{align*}

定义:

h(A, B) = \sum_{t=1}^{\min(A,B)} \left\lfloor \sqrt{\frac{A}{t}} \right\rfloor \left\lfloor \sqrt{\frac{B}{t}} \right\rfloor

代入式子化简为

\sum_{k=1}^{\min(n,m)} \mu(k) \cdot h\left(\left\lfloor \frac{n}{k^2} \right\rfloor, \left\lfloor \frac{m}{k^2} \right\rfloor\right)

双重整除分块+线性筛求出即可。