HDU - 6314:Matrix (广义容斥)(占位)

Samwell Tarly is learning to draw a magical matrix to protect himself from the White Walkers.
the magical matrix is a matrix with n rows and m columns, and every single block should be painted either black or white.
Sam wants to know how many ways to paint the matrix, satisfied that the final matrix has at least A rows, B columns was painted completely black. Cause the answer might be too big, you only need to output it modulo 998244353.

InputThere might be multiple test cases, no more than 5. You need to read till the end of input.
For each test case, a line containing four integers n,m,A,B.
1≤n,m,A,B≤3000 1≤n,m,A,B≤3000

.
OutputFor each test case, output a line containing the answer modulo 998244353.
Sample Input

3 4 1 2

Sample Output

169

题意:给N*M的空白格子染色,求至少x行,至少y列被染色的方案数。

思路:不会,占位。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 3001;
const int MOD = 998244353;
int n, m, A, B, ans;
int C[maxn][maxn], two[maxn * maxn];
int fa[maxn], fb[maxn];

void Init() {
    for(int i = 0; i < maxn; ++i) {
        for(int j = 0; j <= i; ++j) {
            if(j == i || j == 0) {
                C[i][j] = 1;
            } else {
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
                if(C[i][j] >= MOD) {
                    C[i][j] -= MOD;
                }
            }
        }
    }
    two[0] = 1;
    for(int i = 1; i < maxn * maxn; ++i) {
        two[i] = two[i - 1] * 2;
        if(two[i] >= MOD) {
            two[i] -= MOD;
        }
    }
}

int main() {
    Init();
    while(~scanf("%d%d%d%d", &n, &m, &A, &B)) {
        ans = 0;
        for(int i = A; i <= n; ++i) {
            fa[i] = 0;
            for(int j = A; j < i; ++j) {
                fa[i] = (fa[i] + (LL)C[i][j] * fa[j]) % MOD;
            }
            fa[i] = 1 - fa[i];
            if(fa[i] < 0) {
                fa[i] += MOD;
            }
        }
        for(int i = B; i <= m; ++i) {
            fb[i] = 0;
            for(int j = B; j < i; ++j) {
                fb[i] = (fb[i] + (LL)C[i][j] * fb[j]) % MOD;
            }
            fb[i] = 1 - fb[i];
            if(fb[i] < 0) {
                fb[i] += MOD;
            }
        }
        for(int i = A; i <= n; ++i) {
            LL tmp = (LL)fa[i] * C[n][i] % MOD;
            for(int j = B; j <= m; ++j) {
                ans = (ans + ((tmp * fb[j] % MOD) * C[m][j] % MOD) * two[(n - i) * (m - j)] % MOD) % MOD;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/hua-dong/p/9851697.html

时间: 2024-08-30 16:54:18

HDU - 6314:Matrix (广义容斥)(占位)的相关文章

「总结」容斥。三.广义容斥

首先让我们考虑反演的真正原理. $fr.$反演原理 对于两个函数$f$和$g$. 我们知道: $$g(n)=\sum\limits_{i=0}^{n}a_{n,i}f(i)$$ $$f(n)=\sum\limits_{i=0}^{n}b_{n,i}g(i)$$ 将第一个式子代入第二个. $$\begin{array}{rcl}\\f(n)&=&\sum\limits_{i=0}^{n}b_{n,i}\sum\limits_{j=0}^{i}a_{i,j}f(j)\\&=&\

HDU - 6314 Matrix(广义容斥原理)

http://acm.hdu.edu.cn/showproblem.php?pid=6314 题意 对于n*m的方格,每个格子只能涂两种颜色,问至少有A列和B行都为黑色的方案数是多少. 分析 参考https://blog.csdn.net/IcePrincess_1968/article/details/81255138 重点在于计算容斥系数. #include <iostream> #include <cstdio> #include <cstdlib> #inclu

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

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 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 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 4609 3-idiots FFT+容斥

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

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

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

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