Problem Description
Suppose there is a h × w h×w h×w grid consisting of empty or full cells. Let’s make some definitions:
- 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) (1≤i≤h). In particular, r i = 0 ri=0 ri=0 if the leftmost cell of the i i i-th row is empty.
- 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) (1≤j≤w). 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})
(1≤h,w≤103) — 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(0≤ri≤w) — 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(0≤cj≤h) — 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;
}
实践是检验真理的唯一标准