我有一段代码:
输入N,求G(N)的值.
sol
$sumlimits_{i=1}^{n} sumlimits_{j=1}^{n/i} h[i imes j]+=[gcd(i,j)==1] $
$sumlimits_{d} sumlimits_{i=1}^{n/d} sumlimits_{j=1}^{n/id} h[ijd^2]+=mu(d)$
由于我们求$sum h$
$calc(x)=sumlimits_{i=1}^{n} sumlimits_{j=1}^{n/i} 1$
$sumlimits_{d} sumlimits_{i=1}^{n/d} sumlimits_{j=1}^{n/id} mu(d)*calc(n/d^2)$
前面d的枚举是$sqrt(n)$的 ,calc整数分块
那么效率是$sqrt{n} +sqrt{n/4}+sqrt{n/9}...$
感觉是$ sqrt{n} ln(n)$
#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #define maxn 100005 #define mod 998244353 using namespace std; int n,N,pri[maxn],flag[maxn],tot,mu[maxn],ans; int calc(int x){ int sum=0; for(int i=1,nex;i<=x;i=nex+1){ nex=x/(x/i); sum=(sum+1LL*(nex-i+1)*(x/i)%mod)%mod; } return sum; } int main(){ cin>>n;N=sqrt(n)+1;mu[1]=1; for(int i=2;i<=N;i++){ if(!flag[i]){pri[++tot]=i;mu[i]=-1;} for(int j=1;j<=tot&&i*pri[j]<=N;j++){ flag[i*pri[j]]=1; if(i%pri[j]==0){mu[i*pri[j]]=0;break;} mu[i*pri[j]]=-mu[i]; } } for(int i=1;i<=N;i++){ if(mu[i]!=0){ ans=(ans+mu[i]*calc(n/(i*i)))%mod; } } ans=(ans+mod)%mod; cout<<ans<<endl; return 0; }