题目链接:https://loj.ac/problem/528
题目:给定两个正整数N,M,你需要计算ΣΣu(gcd(i,j))^2 mod 998244353 ,其中i属于[1,N],j属于[1,M]
解题思路:
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e7+;
const int mod=;
ll n,m,mu[maxn],sum[maxn],prime[maxn],tot;
void getMobius(int N){
for(int i=;i<=N;i++)prime[i]=;
mu[]=;
for(int i=;i<=N;i++){
if(prime[i]){
prime[tot++]=i;
mu[i]=-;
}
for(int j=;j<tot&&i*prime[j]<=N;j++){
prime[i*prime[j]]=;
if(i%prime[j]==){
mu[i*prime[j]]=;
break;
}
mu[i*prime[j]]=-mu[i];
}
}
}
ll solve(ll a,ll b){
ll res=;
for(ll l=,r;l<=a;l=r+){
r=min(a/(a/l),b/(b/l));
ll x=(sum[(int)sqrt(r)]-sum[(int)sqrt(l-)]+mod)%mod,y=(a/l)%mod,z=(b/l)%mod;
res=(res+x*y%mod*z%mod)%mod;
}
return res;
}
int main(){
scanf("%lld%lld",&n,&m);
if(n>m) swap(n,m);
getMobius(1e7);
sum[]=;
for(int i=;i<=1e7;i++) sum[i]=sum[i-]+mu[i];
printf("%lld\n",solve(n,m));
return ;
}