#include <bits/stdc++.h>#define int long longusing 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,求序列数。
#include <bits/stdc++.h>#define int long longusing 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;
}