对于gcd(i,j)的位置来说,对答案的贡献是2*(gcd(i,j)-1)+1,所以答案ans
ans=Σ(1<=i<=n)(1<=j<=m)2*(gcd(i,j)-1)+1
ans=2*Σ(1<=i<=n)(1<=j<=m)gcd(i,j)-n*m
前者可以通过莫比乌斯反演来计算,便很容易得出答案
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=100000+10,maxn=400000+10,inf=0x3f3f3f3f; int mu[N],prime[N],sum[N]; bool mark[N]; int num[N]; void init() { mu[1]=1; int cnt=0; for(int i=2;i<N;i++) { if(!mark[i])prime[++cnt]=i,mu[i]=-1,num[i]=1; for(int j=1;j<=cnt;j++) { int t=i*prime[j]; if(t>N)break; mark[t]=1; num[t]=num[i]+1; if(i%prime[j]==0){mu[t]=0;break;} else mu[t]=-mu[i]; } } for(int i=1;i<N;i++)sum[i]=sum[i-1]+mu[i]; } int main() { init(); int n,m; scanf("%d%d",&n,&m); ll ans=0; for(int j=1;j<=max(n,m);j++) { ll te=0; int ten=n/j,tem=m/j; for(int i=1,last;i<=min(ten,tem);i=last+1) { last=min(ten/(ten/i),tem/(tem/i)); te+=(ll)(sum[last]-sum[i-1])*(ten/i)*(tem/i); } // printf("%lld\n",te); ans+=te*j; } printf("%lld\n",2*ans-(ll)n*m); return 0; } /******************** ********************/