NOIP 模拟 $26\; \rm 神炎皇$
题解 \(by\;zj\varphi\)
一道 \(\varphi()\) 的题。
对于一个合法的数对,设它为 \((a*m,b*m)\) 则 \(((a+b)*m)|a*b*m^2\),所以 \((a+b)|a*b\),因为 \(\gcd(a,b)=1\),所以 \(a+b|m\)
那么设 \(a+b=k\),且 \(k*m\le n\),那么 \(k\) 最大为 \(\sqrt n\),所以枚举 \(k\) 即可,对于每个 \(k\),有 \(\rm \frac{n}{k^2}\) 个 \(m\) ,同时又有 \(\rm \varphi(k)\) 对 \(a+b\)
Code:
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
using namespace std;
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf,OPUT[100];
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
template<typename T>inline void read(T &x) {
ri f=1;x=0;register char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=0;ch=gc();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
x=f?x:-x;
}
template<typename T>inline void print(T x) {
if (x<0) putchar('-'),x=-x;
if (!x) return putchar('0'),(void)putchar('\n');
ri cnt(0);
while(x) OPUT[p(cnt)]=x%10,x/=10;
for (ri i(cnt);i;--i) putchar(OPUT[i]^48);
return (void)putchar('\n');
}
}
using IO::read;using IO::print;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
typedef long long ll;
static const int N=1e7+7;
int vis[N],phi[N],prim[N],nm[N],cnt,sn;
ll n,ans;
inline void Getphi(int n) {
for (ri i(2);i<=n;p(i)) {
if (!vis[i]) phi[i]=i-1,vis[prim[p(cnt)]=i]=i;
for (ri j(1);j<=cnt&&prim[j]*i<=n;p(j)) {
int nw=i*prim[j];
vis[nw]=prim[j];
if (vis[i]==prim[j]) {
phi[nw]=phi[i]*prim[j];
break;
}
else phi[nw]=phi[i]*(prim[j]-1);
}
}
}
inline int main() {
//FI=freopen("nanfeng.in","r",stdin);
//FO=freopen("nf.out","w",stdout);
read(n);
sn=sqrt(n);
Getphi(sn);
for (ri i(2);i<=sn;p(i)) ans+=(ll)phi[i]*(n/i/i);
print(ans);
return 0;
}
}
int main() {return nanfeng::main();}