cf900D. Unusual Sequences(容斥 莫比烏斯反演)

自為風月馬前卒發表於2019-01-27

題意

題目連結

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 << `
`;}
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;
}

相關文章