分析
设 $\mathbf{f}(i)=i^k$,$\mathbf{g}(i)=\mathbf{f}*\mu$ 。
$$
\begin{aligned}
&\sum_{i=1}^n\sum_{j=1}^m\mathbf{f}(\gcd(i,j))\\
=&\sum_{i=1}^n\sum_{j=1}^m\sum_{d|i,d|j}\mathbf{g}(d)\\
=&\sum_{d=1}^n\mathbf{g}(d)\Big\lfloor\frac{n}{d}\Big\rfloor\Big\lfloor\frac{m}{d}\Big\rfloor
\end{aligned}
$$
线性筛 $\mathbf{g}$ 的前缀和,然后数论分块计算即可。
代码
//It is made by M_sea
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
typedef long long ll;
using namespace std;
inline int read() {
int X=0,w=1; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
return X*w;
}
const int N=5000000+10;
const int mod=1e9+7;
inline int qpow(int a,int b) {
int ans=1;
for (;b;b>>=1,a=1ll*a*a%mod)
if (b&1) ans=1ll*ans*a%mod;
return ans;
}
int n,m,k;
int p[N],v[N],tot;
int f[N],s[N],sum[N];
inline void sieve(int n) {
v[1]=1,f[1]=1;
for (re int i=2;i<=n;++i) {
if (!v[i]) p[++tot]=i,s[tot]=qpow(i,k),f[i]=s[tot]-1;
for (re int j=1;j<=tot&&i*p[j]<=n;++j) {
v[i*p[j]]=1;
if (i%p[j]==0) { f[i*p[j]]=1ll*f[i]*s[j]%mod; break; }
else f[i*p[j]]=1ll*f[i]*f[p[j]]%mod;
}
}
for (re int i=1;i<=n;++i) sum[i]=(sum[i-1]+f[i])%mod;
}
int main() {
int T=read(); k=read(); sieve(5e6);
while (T--) {
n=read(),m=read();
if (n>m) swap(n,m);
ll ans=0;
for (re int l=1,r;l<=n;l=r+1) {
r=min(n/(n/l),m/(m/l));
ans=(ans+1ll*(n/l)*(m/l)%mod*(sum[r]-sum[l-1])%mod)%mod;
}
printf("%lld\n",(ans+mod)%mod);
}
return 0;
}