B. Filling the Grid codeforces

题目链接:https://codeforces.com/contest/1228/problem/B

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#define mem(a,b) memset(a,b,sizeof(a))

using namespace std;
string a[1005];
const int mod = 1e9+7;
int dir[4][2] = {0,1,0,-1,1,0,-1,0};
 int main()
{
    int h,w,r[1005],c[1005],ans[1005][1005];
    while(cin >> h >> w) {
        int flag = 0;
        memset(ans,0,sizeof(ans));
        for(int i = 1; i <= h;i++) {
            cin >> r[i];
        }
        for(int i = 1; i <= w; i++) {
            cin >> c[i];
        }
        //1 代表空 2 代表满
        for(int i = 1; i <= h; i++) {
            if(r[i] == 0) {
                ans[i][1] = 1;
                continue;
            }
            for(int j = 1; j <= r[i];j++) {
                ans[i][j] = 2;
            }
            if(r[i] < w)//让最后一个满的下一个为空
                ans[i][r[i]+1] = 1;
        }
        for(int i = 1; i <= w; i++) {
            if(c[i] == 0){
                if(ans[1][i] == 2) {
                    flag = 1;
                    break;
                }
                ans[1][i] = 1;
                continue;
            }
            for(int j = 1; j <= c[i]; j++) {
                if(ans[j][i] == 1) {
                    flag = 1;
                    break;
                }
                ans[j][i] = 2;
            }
            if(flag == 1)
                break;
            if(c[i] < h)
            {
                if(ans[c[i]+1][i] == 2)
                {
                    flag = 1;
                    break;
                }
                ans[c[i]+1][i] = 1;
            }
        }
        int sum = 0;
        for(int i = 1; i <= h; i++) {
            for(int j = 1; j <= w; j++) {
                if(ans[i][j] == 0)
                    sum++;
            }
        }
        int anss = 0;
        for(int i = 0; i <= sum; i++)
            if(i == 0)
                anss = 1;
            else {
                anss*=2;
                anss %= mod;
            }
        if(flag == 1) anss = 0; //如果不满足条件break掉的 答案为0
        cout << anss << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/LLLAIH/p/11621722.html

时间: 2024-08-30 17:53:45

B. Filling the Grid codeforces的相关文章

[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数.要保证每行每列的格子上的数最小值为1,有多少种方案 \(n \leq 250,k \leq 10^9\) 分析 这题有\(O(n^3)\)的dp做法,但个人感觉不如\(O(n^2 \log n)\)直接用数学方法求更好理解. 考虑容斥原理,枚举有\(i\)行最小值>1,有\(j\)行最小值>1,那

Another Filling the Grid

E. Another Filling the Grid 参考:Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 容斥这个东西可以理解,但是运用到实际的时候,还是觉得有点迷迷糊糊的,不知道套公式会不会是一种可行的办法. 是时候也得把以前的知识温习一下了.... 具体的思路看参考的博客就可以理解了. 代码: // Created by CAD on 2019/10/2. #include <bits/stdc++.h>

Codeforces Round #589 (Div. 2) B——B. Filling the Grid

Suppose there is a h×wh×w grid consisting of empty or full cells. Let's make some definitions: riri is the number of consecutive full cells connected to the left side in the ii-th row (1≤i≤h1≤i≤h). In particular, ri=0ri=0 if the leftmost cell of the 

Codeforces 1228E. Another Filling the Grid

传送门 看到 $n=250$ 显然考虑 $n^3$ 的 $dp$ 设 $f[i][j]$ 表示填完前 $i$ 行,目前有 $j$ 列的最小值是 $1$ 的合法方案数 那么对于 $f[i][j]$ ,枚举 $f[i-1][k]$ ,有 $f[i][j]=\sum_{k=0}^{j}\binom{n-k}{j-k}f[i-1][k](m-1)^{n-j}m^k$ 这里 $m$ 就是题目的 $k$ $\binom{n-k}{j-k}$ 是因为多出来的 $j-k$ 列 $1$ 可以任选 $(m-1)^{

Codeforces Round #589 (Div. 2) Another Filling the Grid (dp)

题意:问有多少种组合方法让每一行每一列最小值都是1 思路:我们可以以行为转移的状态 附加一维限制还有多少列最小值大于1 这样我们就可以不重不漏的按照状态转移 但是复杂度确实不大行(减了两个常数卡过去的...) #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const int N = 3e5+7; typedef long long ll; co

E. Another Filling the Grid 状压dp

http://codeforces.com/contest/1228/my 题意:有个nm的矩形  每个格子可以取1-k中的一种数字  问有多少种填法  使得每行每列至少都有一个1 题解:设置dp[i][j] 表示 当前处理到i行有j列为1的方案数   然后统计答案贡献即可   注意改行至少取一个1 #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a);i<=(b);i++) #defi

CF1228E Another Filling the Grid

Description 题目链接 把 \(k\) 个数填进 \(n\times n\) 的网格中,要求每行每列的最小值均为 \(1\) ,求合法方案数对 \(10^9+7\) 取模的结果 \(1\le n\le 250,1\le k\le 10^9\) Solution 看着标签是 \(\text{combinatorics}\) 和 \(\text{dp}\) 就看了看题目...... 考虑从左向右 \(\text{dp}\) ,每列至少有一个 \(1\) ,同时考虑行的情况.对于某一列如果填

Codeforces Round #589 (Div. 2) (e、f没写)

https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每一位的数字不相同,找出满足要求的最小的那个数输出,没有找到就输出-1: 1 #include<bits/stdc++.h> 2 using namespace std; 3 bool check(int n){ 4 int vis[15]={0}; 5 while(n){ 6 if(!vis[n%

Codeforces Round #589 (Div. 2)

目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Complete Tripartite E. Another Filling the Grid Contest Info Practice Link Solved A B C D E F 5/6 O O O O ? - O 在比赛中通过 ? 赛后通过 ! 尝试了但是失败了 - 没有尝试 Solutions