码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-27 18:57:20      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:for   rac   clu   容斥   mat   http   typename   https   复杂   

题意

题目链接

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

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

标签:for   rac   clu   容斥   mat   http   typename   https   复杂   

原文地址:https://www.cnblogs.com/zwfymqz/p/10327298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!