题解 P4388 【付公主的矩形】
y2823774827y · · 题解
更好的阅读体验
前置芝士
那
其中
所以我们能推倒出公式
则我们要求
设
我们枚举
欧拉函数有一性质
跟这个推倒过程差不多,易得出
跑得比较慢(200ms)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn=1000007;
LL n,tot,ans;
LL phi[maxn],pim[maxn>>1];
inline void First(){
for(LL i=2;i<=n+1;i++){
if(!phi[i])
phi[i]=i-1,
pim[++tot]=i;
for(LL j=1;j<=tot&&pim[j]*i<maxn;j++)
if(i%pim[j]==0){
phi[i*pim[j]]=phi[i]*pim[j];
break;
}else
phi[i*pim[j]]=phi[i]*(pim[j]-1);
}
}
int main () {
scanf("%lld",&n);
First();
for(LL i=1;i<=n;i++)
if(n%i==0)
ans+=phi[n/i+1];
printf("%lld",ans+1>>1);
return 0;
}
剪一下枝(100ms)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int Read(){
int x=0,f=1; char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
const LL maxn=1000007;
int n,tot;
int phi[maxn],pim[maxn>>1];
LL ans;
inline void First(){
for(int i=2;i<=n+1;i++){
if(!phi[i])
phi[i]=i-1,
pim[++tot]=i;
for(int j=1;j<=tot&&pim[j]*i<maxn;j++)
if(i%pim[j]==0){
phi[i*pim[j]]=phi[i]*pim[j];
break;
}else
phi[i*pim[j]]=phi[i]*(pim[j]-1);
}
}
int main () {
n=Read();
First();
for(int i=1;i*i<=n;i++)
if(n%i==0)
if(i*i==n)
ans+=phi[i+1];
else
ans+=phi[i+1]+phi[n/i+1];
printf("%lld",ans+1>>1);
return 0;
}