• ARC096E Everything on It 容斥原理


    题目传送门

    https://atcoder.jp/contests/arc096/tasks/arc096_c

    题解

    考虑容斥,问题转化为求至少有 (i) 个数出现不高于 (1) 次。

    那么我们令这 (i) 个数被划分到 (j) 个集合中。但是由于限制是不多于一次,意味着可能存在一些数没有出现过。那么,我们计算的时候可以将这种情况看成新增一个数 (0),然后将这 (i+1) 个数划分到 (j+1) 个集合中,与 (0) 在同一个集合的表示没有出现过。于是将 (i) 个数中的一些数划分到 (j) 个集合的方案数为 (egin{Bmatrix} i + 1 \ j + 1 end{Bmatrix})

    然后考虑剩下来的 (n - i) 个数可以形成 (2^{n-i}) 个集合。我们可以枚举这些集合有没有出现,那么就是 (2^{2^{n-i}})。最后剩下的 (n-i) 个数还可以往之前的 (j) 个集合里面贴,所以再乘上 ((2^{n-i})^j)

    于是最后的答案为:

    [sum_{i=0}^n (-1)^i inom ni sum_{j=0}^i egin{Bmatrix} i + 1 \ j + 1 end{Bmatrix} cdot 2^{2^{n-i}} cdot (2^{n-i})^j ]

    下面是代码,由于乘方都可以被预处理,所以时间复杂度为 (O(n^2))

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I>
    inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 3000 + 7;
    
    int n, P;
    int S[N][N], C[N][N];
    
    inline int smod(int x) { return x >= P ? x - P : x; }
    inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
    inline int fpow(int x, int y, const int &P = ::P) {
    	int ans = 1;
    	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
    	return ans;
    }
    
    inline void ycl() {
    	S[0][0] = C[0][0] = 1;
    	for (int i = 1; i <= n + 1; ++i) {
    		C[i][0] = 1;
    		for (int j = 1; j <= i; ++j) S[i][j] = (S[i - 1][j - 1] + (ll)S[i - 1][j] * j) % P, C[i][j] = smod(C[i - 1][j - 1] + C[i - 1][j]);
    	}
    }
    
    inline void work() {
    	ycl();
    	int ans = 0;
    	for (int i = 0; i <= n; ++i) {
    		int cnt = 0, ni22 = fpow(2, fpow(2, n - i, P - 1)), fn1 = fpow(2, n - i), fn = 1;
    		for (int j = 0; j <= i; ++j) sadd(cnt, (ll)S[i + 1][j + 1] * ni22 % P * fn % P), fn = (ll)fn * fn1 % P;
    //		dbg("i = %d, ni22 = %d, fn1 = %d, cnt = %d
    ", i, ni22, fn1, cnt);
    		if (i & 1) sadd(ans, P - (ll)cnt * C[n][i] % P);
    		else sadd(ans, (ll)cnt * C[n][i] % P);
    	}
    	printf("%d
    ", ans);
    }
    
    inline void init() {
    	read(n), read(P);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    Java数组的内存结构
    2014.11.9--最近的小感悟
    十一两天感悟
    Are you happiness
    Are you busy?
    Lesson 81-82 Are they true
    Lesson 79-80 People are getting sick
    Lesson 77-78 Socially Discriminated Against
    Lesson 75-76 Shopping on the web
    更新单点,求区间—— luogu 3374
  • 原文地址:https://www.cnblogs.com/hankeke/p/ARC096E.html
Copyright © 2020-2023  润新知