【容斥原理】1717 题解

来源:互联网 发布:毕业生户口迁移 知乎 编辑:程序博客网 时间:2024/05/22 04:52

A=A1A2A3Am
|A|=mi=1|Ai|i<j|AiAj|+i<j<k|AiAjAk|++(1)m1|A1A2A3Am|

#include <iostream>#include <sstream>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#include <cstring>#include <algorithm>#ifndef WIN32#define Auto "%lld"#else#define Auto "%I64d"#endifusing namespace std;#define ll long longll gcd(ll a, ll b) {    return (!b) ? (a) : (gcd(b, a % b));}int n, m;int *arr;ll res = 0;inline void init() {    int bn;    scanf("%d%d", &bn, &m);    arr = new int[(bn + 1)];    for(int i = 1, x; i <= bn; i++) {        scanf("%d", &x);        if(x > 17)            arr[++n] = x;    }}void dfs(int dep, int flag, ll temp) {    if(temp > m)    return;    if(dep == n + 1) {        if(temp > 1)            res += flag * (m / temp);        return;    }    int g = gcd(temp, arr[dep]);    dfs(dep + 1, flag, temp);    dfs(dep + 1, flag * -1, temp / g * arr[dep]);}inline void solve() {    res = (m >= 17 && n) ? (1) : (0);    m -= 17;    dfs(1, -1, 1);    printf(Auto, res);}int main() {    init();    solve();    return 0;}