Filling the Grid CodeForces - 1228B

Problem Description

Suppose there is a h × w h×w h×w grid consisting of empty or full cells. Let’s make some definitions:

  1. r i r_{i} ri is the number of consecutive full cells connected to the left side in the i i i-th row ( 1 ≤ i ≤ h ) (1≤i≤h) (1ih). In particular, r i = 0 ri=0 ri=0 if the leftmost cell of the i i i-th row is empty.
  2. c j cj cj is the number of consecutive full cells connected to the top end in the j j j-th column ( 1 ≤ j ≤ w ) (1≤j≤w) (1jw). In particular, c j = 0 cj=0 cj=0 if the topmost cell of the j j j-th column is empty.

In other words, the i i i-th row starts exactly with r i ri ri full cells. Similarly, the j j j-th column starts exactly with c j cj cj full cells.
在这里插入图片描述
These are the r r r and c c c values of some 3 × 4 3×4 3×4 grid. Black cells are full and white cells are empty.
You have values of r r r and c c c. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of r r r and c c c. Since the answer can be very large, find the answer modulo 1000000007 ( 1 0 9 + 7 ) 1000000007(10^{9}+7) 1000000007(109+7) . In other words, find the remainder after division of the answer by 1000000007 ( 1 0 9 + 7 ) 1000000007(10^{9}+7) 1000000007(109+7).

Input

The first line contains two integers h h h and w w w ( 1 ≤ h , w ≤ 1 0 3 ) (1≤h,w≤10^{3}) (1h,w103) — the height and width of the grid.
The second line contains hh integers r 1 , r 2 , … , r h ( 0 ≤ r i ≤ w ) r_{1},r_{2},…,r_{h}(0≤r^{i}≤w) r1,r2,,rh(0riw) — the values of r r r.
The third line contains w w w integers c 1 , c 2 , … , c w ( 0 ≤ c j ≤ h ) c_{1},c_{2},…,c_{w}(0≤cj≤h) c1,c2,,cw(0cjh) — the values of c.

Output

Print the answer modulo 1000000007 ( 1 0 9 + 7 ) 1000000007(10^{9}+7) 1000000007(109+7).

Examples

Input

3 4
0 3 1
0 2 3 0

Output

2

Input

1 1
0
1

Output

0

Input

19 16
16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12
6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4

Output

797922655

Note

In the first example, this is the other possible case.
In the second example, it’s impossible to make a grid to satisfy such r r r, c c c values.
In the third example, make sure to print answer modulo ( 1 0 9 + 7 ) (10^{9}+7) (109+7).

题意:

给一个 h × w h × w h×w的矩形,每一单位长宽有一个参数,参数表示这一单位的 h ( w ) h(w) h(w) 所对应的矩形前 r i ( c j ) r_{i}(c_{j}) ri(cj) 有多少个边长为1的正方形是涂黑的,问有多少种涂法。

思路:

从图上可以看出前 r i + 1 r_{i}+1 ri+1 和前 w j + 1 w_{j}+1 wj+1 涂的方式是一定的,只有满足 i > w j i>w_{j} i>wj 并且 j > r i j>r_{i} j>ri的情况,才有两种可能(涂与不涂),把每个点遍历一遍,就能求其结果。如果有行和列有矛盾直接输出 0 0 0;

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=1e9+7;
int a[10010],b[10010];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)scanf("%d",&a[i]);//a[]对应的是列 
	for(int i=0;i<m;i++)scanf("%d",&b[i]);//b[]对应行 
	int ans=1;
	for(int i=0;i<n;i++)
	for(int j=0;j<m;j++){
		if(i==b[j]&&j<a[i]){  // 列黑,行白 
			printf("0\n");
			return 0;
		}
		if(j==a[i]&&i<b[j]){   //行黑,列白 
			printf("0\n");
			return 0;
		}
		if(i>b[j]&&j>a[i])   //可涂可不涂 
		ans=(ans*2)%mod;
	}
	printf("%d\n",ans);
	return 0;
}  

实践是检验真理的唯一标准

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值