soj#552 449E Jzzhu and Squares
分析
https://www.cnblogs.com/Memory-of-winter/p/11209128.html
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6+5;
const int mod = 1e9+7;
int phi[N+3],is[N+3],a[N+3],b[N+3],c[N+3],n,m,t;
signed main(){
int i,j,k,x=0,y=0;
for(i=1;i<=N;i++)phi[i]=i;
for(i=2;i<=N;i++)
if(!is[i])for(j=i;j<=N;j+=i)
is[j]=1,phi[j]=phi[j]/i*(i-1);
for(i=1;i<=N;i++){
x=(x+2*i+2)%mod;
y=(y+2*i*i)%mod;
a[i]=((i*i%mod*i%mod-x*i%mod+y)%mod+mod)%mod;
}
for(i=1;i<=N;i++)
for(j=i;j<=N;j+=i)
a[j]=(a[j]+2ll*i*phi[j/i]%mod)%mod;
for(i=1;i<=N;i++){
b[i]=(b[i-1]+a[i]*i%mod)%mod;
c[i]=(c[i-1]+a[i]*i%mod*i%mod)%mod;
a[i]=(a[i]+a[i-1])%mod;
}
scanf("%lld",&t);
while(t--){
scanf("%lld%lld",&n,&m);
n++,m++;
int x=min(n,m);
printf("%lld\n",((n*m%mod*a[x]%mod-(n+m)*b[x]%mod+c[x])%mod+mod)%mod);
}
return 0;
}