20.11.13模拟
作者:互联网
给定一个n,求a+b=n,a+b \ ab 的正整数 数对的数量
设d=gcd(a,b),a=xd,b=yd,所以x+y \ xyd 又因为gcd(x,y)=1 ,所以x+y \ d
因为(x+y)d <=n 所以x+y<=\(\sqrt n\)
所以我们可以枚举k=x+y,因为x,y互质,所以x与x+y互质,所以合法的(x,y)有\(\varphi(k)\)对,合法的d有\(\lfloor \frac{n}{k^2}\rfloor\)
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i(j);i<=k;++i)
#define drp(i,j,k) for(register int i(j);i>=k;--i)
#define repg(x) for(register int i(G.head[x]);i;i=G.next[i])
#define bug cout<<"~~~~~~~~~~~~~"<<'\n';
#define bugout(x) cout<<x<<'\n';
using std::cin;
using std::cout;
typedef long long lxl;
template<typename T>
inline T max( T a, T b) {
return a > b ? a : b;
}
template<typename T>
inline T min( T a, T b) {
return a < b ? a : b;
}
inline char gt() {
static char buf[1 << 21], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
template <typename T>
inline void read(T &x) {
register char ch = gt();
x = 0;
int w(0);
while(!(ch >= '0' && ch <= '9'))w |= ch == '-', ch = gt();
while(ch >= '0' && ch <= '9')x = x * 10 + (ch & 15), ch = gt();
w ? x = ~(x - 1) : x;
}
template <typename T>
inline void out(T x, char cc) {
if(x < 0) x = -x, putchar('-');
char ch[20];
int num(0);
while(x || !num) ch[++num] = x % 10 + '0', x /= 10;
while(num) putchar(ch[num--]);
putchar(cc);
}
const int N = 1e7 + 79;
lxl n;
int phi[N + 55], v[N], prime[N], cnt;
inline void init(int MX) {
phi[1] = 1;
rep(i, 2, MX) {
if(!v[i]) {
v[i] = i;
prime[++cnt] = i;
phi[i] = i - 1;
}
rep(j, 1, cnt) {
if(prime[j] > v[i] || 1ll * prime[j]*i > 1ll * MX) break;
v[i * prime[j]] = prime[j];
phi[i * prime[j]] = phi[i] * (i % prime[j] ? prime[j] - 1 : prime[j]);
}
}
}
int main() {
freopen("c.in", "r", stdin);
freopen("c.out", "w", stdout);
read(n);
int t = sqrt(n);
init(t + 5);
lxl ans(0);
rep(i, 2, t) {
ans += phi[i] * (n / (1ll*i * i));
}
out(ans, '\n');
return 0;
}
标签:prime,13,ch,int,phi,char,inline,20.11,模拟 来源: https://www.cnblogs.com/QQ2519/p/15376817.html