HDU 5321 Beautiful Set 容斥 (看题解)

HDU 5321

感觉有点抗拒这种题目, 看到就感觉自己不会写,其实就是个沙雕题, 感觉得找个时间练练这种题。

g[ i ] 表示gcd为 i 的倍数的方案数, f[ i ] 表示gcd为 i 的方案数, 然后先算g[ i ]然后直接容斥。

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 258280327;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, a[N], cnt[N];

int F[N], Finv[N], inv[N];
int f[N], g[N];

void prepare() {
    F[0] = Finv[0] = inv[1] = 1;
    for(int i = 2; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
    for(int i = 1; i < N; i++) F[i] = 1LL * F[i - 1] * i % mod;
    for(int i = 1; i < N; i++) Finv[i] = 1LL * Finv[i - 1] * inv[i] % mod;
}

inline int A(int n, int m) {
    if(n < 0 || n < m) return 0;
    return 1LL * F[n] * Finv[n - m] % mod;
}

inline int C(int n, int m) {
    if(n < 0 || n < m) return 0;
    return 1LL * F[n] * Finv[m] % mod * Finv[n - m] % mod;
}

int solve1() {
    for(int i = 1; i <= 100000; i++) {
        g[i] = 0;
        for(int j = 1; j <= cnt[i]; j++) {
            add(g[i], 1LL * A(cnt[i], j) * F[n - j + 1] % mod);
        }
    }
    int ans = 0;
    for(int i = 100000; i >= 1; i--) {
        f[i] = g[i];
        for(int j = i + i; j <= 100000; j += i) {
            sub(f[i], f[j]);
        }
        add(ans, 1LL * i * f[i] % mod);
    }
    return ans;
}

int solve2() {
    for(int i = 1; i <= 100000; i++) {
        g[i] = 0;
        for(int j = 1; j <= cnt[i]; j++) {
            add(g[i], 1LL * j * C(cnt[i], j) % mod);
        }
    }
    int ans = 0;
    for(int i = 100000; i >= 1; i--) {
        f[i] = g[i];
        for(int j = i + i; j <= 100000; j += i) {
            sub(f[i], f[j]);
        }
        add(ans, 1LL * i * f[i] % mod);
    }
    return ans;
}

void init() {
    for(int i = 1; i < N; i++) {
        cnt[i] = 0;
    }
}

int main() {
    prepare();
    while(scanf("%d", &n) != EOF) {
        init();
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            cnt[a[i]]++;
        }
        for(int i = 1; i <= 100000; i++) {
            for(int j = i + i; j <= 100000; j += i) {
                cnt[i] += cnt[j];
            }
        }

        int ans1 = solve1();
        int ans2 = solve2();

        if(ans1 == ans2) {
            printf("Equal %d\n", ans1);
        }
        else if(ans1 > ans2) {
            printf("Mr. Zstu %d\n", ans1);
        }
        else {
            printf("Mr. Hdu %d\n", ans2);
        }
    }
    return 0;
}

/*
*/

原文地址:https://www.cnblogs.com/CJLHY/p/11366971.html

时间: 2024-10-02 10:51:37

HDU 5321 Beautiful Set 容斥 (看题解)的相关文章

HDU 2841 Visible Trees(容斥)题解

题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd(a,b)!= 1,那么就会被遮住.因为斜率k一样,后面的点会被遮住,如果有gcd,那么除一下就会变成gcd = 1的那个点的斜率了.所以问题转化为求gcd不为1有几个点,固定一个点,然后容斥. #include<set> #include<map> #include<queue

HDU 4135 Co-prime(容斥:二进制解法)题解

题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也就是有除1的公因数.那么我们把n质因数分解,就能算出含某些公因数的不互质的个数.因为会重复,所以容斥解决.因为因数个数可能很多(随便算了一个20!> 2e18,所以质因数分解个数不会超过20个),我们可以用二进制来遍历解决. #include<set> #include<map>

题解报告:hdu 4135 Co-prime(容斥定理入门)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

HDU 3970 Harmonious Set 容斥欧拉函数

链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中任意选一些数使得选出的数和为n的倍数的方法数 ...并不会如何递推.. 思路: 然后这是公式:点击打开链接 a(n) = 1/n * sum_{d divides n and d is odd} 2^(n/d) * phi(d). d最大是n,也就是1e9,要计算1e9的phi,所以容斥来算phi. #pragma comment(linker, "/STA

HDU 4609 3-idiots FFT+容斥

一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完全可以母函数,无奈数据很大,就用FFT了 然后难点在于最后的统计,要减去自身,两个都大的,一大一小,包含自身,这是用到了容斥,再做相似的题的时候,应该多看看这方面 注:再次高度仰慕kuangbin神,这是我FFT的第二题,也是第二次用kuangbin FFT模板 #include <stdio.h>

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 4135 Co-prime(组合+容斥)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relatively prime if they have no common positive divisors other tha

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y