P2257 YY的GCD题解
P.S.为了方便阐释,下文中定义
分析
-
首先考虑如何计算。
-
-
考虑固定质数
p ,不妨令x=pa ,y=pb ,则\gcd(x,y)=p\iff\gcd(a,b)=1 ,那么问题就转化为统计互质对数。 -
\sum_{\substack{p\leq\min(n,m)\\p\text{ 为质数}}}\sum_{a\leq \lfloor n/p\rfloor}\sum_{b\leq \lfloor m/p\rfloor}[\gcd(a,b)=1] -
这就是我们
熟悉的莫比乌斯反演。 -
-
\sum_{\substack{p\leq\min(n,m)\\p\text{ 为质数}}}\sum_{d \leq \min(\lfloor n/p \rfloor, \lfloor m/p \rfloor)}\mu(d)\lfloor \frac{n} {pd}\rfloor\lfloor \frac{m} {pd}\rfloor -
这就是我们的第一个暴力式子,可得
60pts 。 -
for (auto p : prime) { if (p > min(n, m)) break; ll x = n / p, y = m / p; for (int l = 1, r; l <= min(x, y); l = r + 1) { r = min(x / (x / l), y / (y / l)); ans += (sum[r] - sum[l - 1]) * (x / l) * (y / l); } } -
这里的
sum是对于mu的前缀和,具体见 P3455。 -
这样的复杂度大致为是
O\left(\pi(\min(n,m))\sqrt{\min(n,m)}\right) 。 -
考虑优化。
-
可以交换一下求和顺序,得到
-
\sum_{d=1}^{\min(\lfloor \frac{n}{2} \rfloor, \lfloor \frac{m}{2} \rfloor)}\mu(d)\sum_{\substack{p \leq \min(\lfloor \frac{n}{d} \rfloor, \lfloor \frac{m}{d} \rfloor)\\p \text{ 是质数}}} \lfloor\frac{n}{pd}\rfloor\lfloor\frac{m}{pd}\rfloor -
可将所有乘积相同的项合并。不妨令
i = pd 。观察式子可以发现,合并后还缺少一项系数,因为对于相同的i ,可能存在多个满足pd = i 的组合,而这些组合对应的\mu(d) 不一定相同。因此,我们定义合并后的贡献,她的系数为c(i)=\sum_{\substack{p\mid i\\p\text{ 是质数}}}\mu(i/p) 。上式就可以整理为 -
\sum_{i=1}^{\min(n,m)}c(i)\left\lfloor\frac ni\right\rfloor\left\lfloor\frac mi\right\rfloor -
因此我们只需要预处理一下
c(i) 即可。 -
for (auto p : prime) for (ll i = 1; i * p <= 10000000; i++) c[i * p] += mu[i]; -
预处理复杂度为
O\left(N\sum_{p\le N}\frac1p\right)=O(N\log\log N) ,总复杂度就是O(N\log\log N + T \sqrt{min(n, m)}) 。代码
-
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define D(x) cout << #x << ": " << x << ' ' #define DE(x) cout << #x << ": " << x << '\n' #define pii pair<ll, ll> #define mkp(x, y) make_pair(x, y) #define pl p << 1 #define pr p << 1 | 1 using namespace std; namespace fast_IO { #define IOSIZE 100000 unsigned int precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; char obuf[IOSIZE], *p3 = obuf; char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf; #define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++)) #define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x) template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47 and ch<58) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47 and ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; } template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } inline bool read(char &s) { while (s = getchar(), isspace(s) and s != EOF); return s != EOF; } inline void print(char x) { putchar(x); } inline bool read(char *s) { char ch; while (ch = getchar(), (ch<33) && ch != EOF); if (ch == EOF) return false; while (!(ch<33) and (ch != EOF)) *s++ = ch, ch = getchar(); *s = '\0'; return true; } inline void print(char *x) { while (*x) putchar(*x++); } inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); } inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), (ch<33) and ch != EOF); if (ch == EOF) return false; while (!(ch<33)) s += ch, ch = getchar(); return true; } inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); } inline bool read(bool &b) { char ch; while(ch=getchar(), (ch<33) and ch != EOF); return ch == EOF ?false :(b=ch^48, true); } inline void print(bool b) { putchar(b+48); } inline bool read(double &x) { char ch = getchar(); int f = 1; for(x=0; (ch<48 or 57<ch) and (ch != EOF); ch=getchar()) if(ch == '-') f = -1; if(ch == EOF) return false; for(x=0; 47<ch and ch<58; ch=getchar()) x = x*10 + (ch^48); if(ch != '.') return x *= f, true; double y = 0.1; for(ch=getchar(); 47<ch and ch<58; ch=getchar()) x += y*(ch^48), y /= 10; return x *= f, true; } inline void print(double x) { if(x < 0) putchar('-'), x = -x; if(!precision) print((unsigned long long)(x-(unsigned long long)(x)>=0.5 ?x+1 :x)); else { unsigned long long xx = x; double y = ((x-xx)*POW[precision]); unsigned long long yy = (y-(unsigned long long)(y)>=0.5 ?y+1 :y); if(yy == POW[precision]) xx++, yy = 0; print(xx), putchar('.'); for(int j=precision-1; ~j; j--) putchar(48+yy/POW[j]%10); } } template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); } template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); } template<typename T> inline T read() { T a; return read(a), a; } struct Fast_IO { bool flag = 1; inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } inline void SetPrecision(int x) { if(x > 9) throw std::runtime_error("Precision too high!"); else if(x < 0) throw std::runtime_error("Precision too low!"); else precision = x; } inline operator bool() { return flag; } } io; Fast_IO& operator >> (Fast_IO &io, char *b) { return io.flag &= read(b), io; } template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return io.flag &= read(b), io; } template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; } #define cout io #define cin io #define endl '\n' } using namespace fast_IO; mt19937_64 rng(chrono ::steady_clock ::now().time_since_epoch().count()); constexpr ll MOD = 998244353ll, inf = 1ll << 60; constexpr double eps = 1e-10; ll tt; ll n, m, k, qq; bitset<10000010> bit; vector<ll> prime; ll xs[10000010]; ll mu[10000010]; ll sum[10000010]; void init() { mu[1] = 1, bit[1] = 1; for (ll i = 2; i <= 10000000; i++) { if (!bit[i]) mu[i] = -1, prime.push_back(i); for (auto j : prime) { if (i * j > 10000000) break; bit[i * j] = 1; if (i % j == 0) { mu[i * j] = 0; break; } mu[i * j] = -mu[i]; } } for (auto p : prime) for (ll i = 1; i * p <= 10000000; i++) xs[i * p] += mu[i]; // DE(prime.size()); 664579 for (int i = 1; i <= 10000000; i++) sum[i] = sum[i - 1] + xs[i]; } void solve() { cin >> n >> m; ll ans = 0; for (int l = 1, r; l <= min(n, m); l = r + 1) { r = min(n / (n / l), m / (m / l)); ans += (sum[r] - sum[l - 1]) * (n / l) * (m / l); } // for (auto p : prime) // { // if (p > min(n, m)) // break; // ll x = n / p, y = m / p; // for (int l = 1, r; l <= min(x, y); l = r + 1) // { // r = min(x / (x / l), y / (y / l)); // ans += (sum[r] - sum[l - 1]) * (x / l) * (y / l); // } // } cout << ans << '\n'; } int main() { // ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); #ifdef HJ freopen("in.txt", "r", stdin); #endif #ifdef LOG freopen("out.txt", "w", stdout); #endif init(); for (cin >> tt; tt--;) solve(); return 0; }