cf900D. Unusual Sequences(容斥 莫比乌斯反演)
程序员文章站
2022-03-26 11:15:40
题意 "题目链接" Sol 首先若y % x不为0则答案为0 否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1 考虑容斥,设$g[i]$表示满足和为$i$的序列的方案数,显然$g[i] = 2^{i 1}$(插板后每空位放不放) 同时还可以枚举一下gcd,设$f[i]$表示满 ......
题意
sol
首先若y % x不为0则答案为0
否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1
考虑容斥,设\(g[i]\)表示满足和为\(i\)的序列的方案数,显然\(g[i] = 2^{i-1}\)(插板后每空位放不放)
同时还可以枚举一下gcd,设\(f[i]\)表示满足和为\(i\)且所有数的gcd为1的方案,\(g[i] = \sum_{d | i} f[\frac{n}{d}]\)
反演一下,\(f[i] = \sum_{d | i} \mu(d) g(\frac{i}{d})\)
mu函数可以暴力枚举质因子得到
复杂度\(o(2^{mx} * mx + \sqrt{n}\) ,\(mx\)最大为10
#include<bits/stdc++.h> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second #define int long long #define ll long long #define fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? eof : *p1++) //char buf[(1 << 22)], *p1 = buf, *p2 = buf; using namespace std; const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10; const double eps = 1e-9; template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;} template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;} template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;} template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;} template <typename a> inline void debug(a a){cout << a << '\n';} template <typename a> inline ll sqr(a x){return 1ll * x * x;} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int x = read(), y = read(); map<int, int> mu; int g(int a, int p) { int base = 1; while(p) { if(p & 1) base = mul(base, a); p >>= 1; a = mul(a, a); } return base; } signed main() { if(y % x != 0) return puts("0"), 0; vector<int> d; y /= x; int p = y; for(int i = 2; i * i <= y; i++) if(!(y % i)) { d.push_back(i); while(!(y % i)) y /= i; } if(y != 1) d.push_back(y); y = p; for(int sta = 0; sta < (1 << d.size()); sta++) { int v = 1, t = 1; for(int i = 0; i < d.size(); i++) if(sta & (1 << i)) t *= -1, v *= d[i]; mu[v] = t; } int ans = 0; for(auto &x: mu) { int d = x.fi, m = x.se; add2(ans, mul(m + mod, g(2, y / d - 1))); } cout << ans; return 0; }