题目链接
https://www.luogu.org/problemnew/show/P4844
题解
即
设,则有
由于
假设,那么,由于,因此或,不可能同时满足,那么,但是,推出矛盾,因此。
那么
如果已经得到了和,满足题目要求的条件就是
容易发现
因此反演求出一段区间内与互质的数的个数即可。
注意这题卡时限,必须预处理出每个数的约数,还要用邻接表存,不能用vector,否则会TLE……
代码
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
template<typename T>
T read()
{
T x=0;
int f=1;
char ch=getchar();
while((ch<'0')||(ch>'9'))
{
if(ch=='-')
{
f=-f;
}
ch=getchar();
}
while((ch>='0')&&(ch<='9'))
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int maxn=1414213;
const int maxm=13288457;
int p[maxn+10],prime[maxn+10],cnt,mu[maxn+10],pre[maxm+10],now[maxn+10],son[maxm+10],tot;
int add(int a,int b)
{
pre[++tot]=now[a];
now[a]=tot;
son[tot]=b;
return 0;
}
int getprime()
{
p[1]=mu[1]=1;
for(int i=2; i<=maxn; ++i)
{
if(!p[i])
{
prime[++cnt]=i;
mu[i]=-1;
}
for(int j=1; (j<=cnt)&&(i*prime[j]<=maxn); ++j)
{
int x=i*prime[j];
p[x]=1;
if(i%prime[j]==0)
{
mu[x]=0;
break;
}
mu[x]=-mu[i];
}
}
for(int i=1; i<=maxn; ++i)
{
if(!mu[i])
{
continue;
}
for(int j=1; j<=maxn/i; ++j)
{
add(i*j,i);
}
}
return 0;
}
inline long long solve(int x,int l,int r)
{
long long ans=0;
for(int i=now[x]; i; i=pre[i])
{
int k=son[i];
ans+=mu[k]*(r/k-l/k);
}
return ans;
}
long long n;
int main()
{
getprime();
n=read<long long>();
long long ans=0;
int mx=sqrt(2*n)+0.5;
for(int i=2; i<=mx; ++i)
{
ans+=solve(i,std::max(1ll,i-n/i)-1,std::min(n/i,i-1ll));
}
printf("%lld\n",ans);
return 0;
}