题面
Sol
运用提出gcd等莫比乌斯反演的推导技巧得到
[ans=sum_{d=1}^{n}d^3sum_{i=1}^{lfloorfrac{n}{d}
floor}mu(i)*i^2*S(lfloorfrac{n}{d*i}
floor)^2
]
其中(S(n)=sum_{i=1}^{n}i=frac{n(n+1)}{2})
用(代替d*i)
(ans=sum_{i=1}^{n}S(lfloorfrac{n}{i}
floor)^2 * i^2 sum_{d|i}d*mu(frac{i}{d}))
然后写线性筛就可以拿到优秀的暴力分
优化?
n这么大,想一想杜教筛
筛的时候发现(sum_{d|i}d*mu(frac{i}{d}))和(varphi(d))的筛法一模一样
好的,那它们两个就相等
所以就是(sum_{i=1}^{n}S(lfloorfrac{n}{i}
floor)^2 * i^2 * varphi(i))
(S(lfloorfrac{n}{i}
floor)^2就分块一下)后面的杜教筛
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(5e6 + 1);
IL ll Read(){
RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int prime[_], num, Zsy, phi[_], inv2, inv6, N;
map <ll, int> Phi;
bool isprime[_];
IL void Prepare(){
isprime[1] = 1; phi[1] = 1;
for(RG int i = 2; i < N; ++i){
if(!isprime[i]){ prime[++num] = i; phi[i] = (i - 1); }
for(RG int j = 1; j <= num && i * prime[j] < N; ++j){
isprime[i * prime[j]] = 1;
if(i % prime[j]) phi[i * prime[j]] = 1LL * phi[i] * (prime[j] - 1) % Zsy;
else{ phi[i * prime[j]] = 1LL * phi[i] * prime[j] % Zsy; break; }
}
}
for(RG int i = 2; i < N; ++i) phi[i] = 1LL * phi[i] * i % Zsy * i % Zsy, (phi[i] += phi[i - 1]) %= Zsy;
}
IL ll Pow(RG ll x, RG ll y){
RG ll ret = 1;
for(; y; x = x * x % Zsy, y >>= 1) if(y & 1) ret = ret * x % Zsy;
return ret;
}
IL ll Ssqr(RG ll x){ return x % Zsy * (x + 1) % Zsy * (2 * x % Zsy + 1) % Zsy * inv6 % Zsy; }
IL ll Sumphi(RG ll n){
if(n < N) return phi[n];
if(Phi[n]) return Phi[n];
RG ll ans = n % Zsy * (n + 1) % Zsy * inv2 % Zsy; ans = ans * ans % Zsy;
for(RG ll i = 2, j; i <= n; i = j + 1){
j = n / (n / i);
ans -= (Ssqr(j) - Ssqr(i - 1)) * Sumphi(n / i) % Zsy;
ans = (ans + Zsy) % Zsy;
}
return Phi[n] = ans;
}
int main(RG int argc, RG char* argv[]){
Zsy = Read(); RG ll n = Read(); N = min(1LL * _, n + 1); Prepare();
inv2 = Pow(2, Zsy - 2); inv6 = Pow(6, Zsy - 2);
RG ll ans = 0, s;
for(RG ll i = 1, j; i <= n; i = j + 1){
j = n / (n / i);
s = (n / i) % Zsy * (n / i + 1) % Zsy * inv2 % Zsy;
s = s * s % Zsy;
ans += s * (Sumphi(j) - Sumphi(i - 1)) % Zsy;
ans = (ans + Zsy) % Zsy;
}
printf("%lld
", ans);
return 0;
}