[egin{split} A&=sum_{i=1}^{n}sum_{j=1}^n{ijgcd(i,j)} \ &=sum_{d=1}^nsum_{i=1}^{lfloorfrac{n}{d}
floor}sum_{j=1}^{lfloorfrac{n}{d}
floor}ijd^3[gcd(i,j)=1]\
&=sum_{d=1}^nd^3sum_{i=1}^{lfloorfrac{n}{d}
floor}sum_{j=1}^{lfloorfrac{n}{d}
floor}ij[gcd(i,j)=1]
end{split}
]
设(f(n)=sum_{i=1}^{n}sum_{j=1}^nij[gcd(i,j)=1]),(s(n)=sum_{i=1}^{n}sum_{j=1}^nij)
则
[egin{split}A&=sum_{d=1}^nd^3f(lfloorfrac{n}{d}
floor)end{split}
]
而
[sum_{i=1}^{n}i^3=(sum_{i=1}^ni)^2=[frac{n(n+1)}{2}]^2
]
有
[egin{split}s(n)&=sum_{i=1}^{n}sum_{j=1}^nij=[frac{n(n+1)}{2}]^2\ &=sum_{i=1}^{n}sum_{j=1}^nij \ &=sum_{t=1}^{n}sum_{i=1}^{lfloorfrac{n}{t}
floor}sum_{j=1}^{lfloorfrac{n}{t}
floor}t^2ij[gcd(i,j)=1]
\
&=sum_{t=1}^{n}t^2f(lfloorfrac{n}{t}
floor)=f(n)+sum_{t>1}t^2f(lfloorfrac{n}{t}
floor)
end{split}]
[f(n)=s(n)-sum_{t>1}t^2f(lfloorfrac{n}{t}
floor)=[frac{n(n+1)}{2}]^2-sum_{t>1}t^2f(lfloorfrac{n}{t}
floor)
]
数论分块即可
[sum_{i=1}^{n}i^2=frac{n(n+1)(2n+1)}{6}
]
预处理 (f(n))
[f(n)=sum_{i=1}^{n}sum_{j=1}^nij[gcd(i,j)=1]=f(n-1)+2sum_{i=1}^{n}in[gcd(i,n)=1]
]
而
[sum_{i=1}^ni[gcd(i,n)=1]=nfrac{varphi(n)}{2}
]
故
[f(n)=f(n-1)+n^2varphi(n)
]
智障选手不小心把 n 也模掉了于是多调了半个小时
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e6+5;
int i2,i4,i6,mod,n;
bool isNotPrime[N + 5];
int mu[N + 5], phi[N + 5], primes[N + 5], cnt;
inline void euler() {
isNotPrime[0] = isNotPrime[1] = true;
mu[1] = 1;
phi[1] = 1;
for (int i = 2; i <= N; i++) {
if (!isNotPrime[i]) {
primes[++cnt] = i;
mu[i] = -1;
phi[i] = i - 1;
}
for (int j = 1; j <= cnt; j++) {
int t = i * primes[j];
if (t > N) break;
isNotPrime[t] = true;
if (i % primes[j] == 0) {
mu[t] = 0;
phi[t] = phi[i] * primes[j];
break;
} else {
mu[t] = -mu[i];
phi[t] = phi[i] * (primes[j] - 1);
}
}
}
}
inline void exgcd(int a,int b,int &x,int &y) {
if(!b) {
x=1,y=0;
return;
}
exgcd(b,a%b,x,y);
int t=x;
x=y,y=t-(a/b)*y;
}
inline int inv(int a,int b) {
int x,y;
return exgcd(a,b,x,y),(x%b+b)%b;
}
int sum[N+1];
int h(int n) { n%=mod;
return n*n%mod*(n+1)%mod*(n+1)%mod*i4%mod;
}
int u(int n) { n%=mod;
return n*(n+1)%mod*(2ll*n+1)%mod*i6%mod;
}
map<int,int> mp;
int S(int n) {
if(n<N) return sum[n];
if(mp[n]) return mp[n];
int l=2, r, ans=h(n);
while(l<=n) {
r=n/(n/l);
ans -= (((u(r)-u(l-1))%mod+mod)%mod*S(n/l))%mod;
ans %= mod;
ans += mod;
ans %= mod;
l=r+1;
}
mp[n]=ans;
return ans;
}
int read() {
long long t;
cin>>t;
return t;
}
signed main() {
ios::sync_with_stdio(false);
mod=read();
n=read();
i2=inv(2,mod);
i4=inv(4,mod);
i6=inv(6,mod);
euler();
sum[0]=phi[0];
for(int i=1;i<=N;i++) sum[i]=(sum[i-1]+phi[i]%mod*i%mod*i%mod)%mod;
int ans = 0, l=1, r;
while(l<=n) {
//cout<<l<<endl;
r=(n/(n/l));
ans += h(n/l) * ((S(r)-S(l-1)+mod)%mod) % mod;
ans %= mod;
l=r+1;
}
cout<<(long long)ans;
}