HYSBZ - 2818莫比乌斯反演

时间:2022-10-27 16:10:47

链接

题意很简洁不说了

题解:一开始我想直接暴力,复杂度是O(log(1e7)*sqrt(1e7))算出来是2e9,可能会复杂度爆炸,但是我看时限是10s,直接大力莽了一发暴力,没想到就过了= =

就是先打出1e7的素数表,然后挨个算即可

//#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-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int mu[N],prime[N],sum[N];
bool mark[N];
int cnt;
void init()
{
mu[]=;
cnt=;
for(int i=;i<N;i++)
{
if(!mark[i])prime[++cnt]=i,mu[i]=-;
for(int j=;j<=cnt;j++)
{
int t=i*prime[j];
if(t>N)break;
mark[t]=;
if(i%prime[j]==){mu[t]=;break;}
else mu[t]=-mu[i];
}
}
for(int i=;i<N;i++)sum[i]=sum[i-]+mu[i];
}
int main()
{
init();
int n;
scanf("%d",&n);
ll ans=;
for(int i=;i<=cnt;i++)
{
int te=n/prime[i];
for(int j=,last;j<=te;j=last+)
{
last=te/(te/j);
ans+=(ll)(sum[last]-sum[j-])*(te/j)*(te/j);
}
}
printf("%lld\n",ans);
return ;
}
/******************** ********************/