Suppose there is a h×w
grid consisting of empty or full cells. Let's make some definitions:
- ri
is the number of consecutive full cells connected to the left side in the i-th row (1≤i≤h). In particular, ri=0 if the leftmost cell of the i
- -th row is empty.
- cj
- is the number of consecutive full cells connected to the top end in the j-th column (1≤j≤w). In particular, cj=0 if the topmost cell of the j
- -th column is empty.
In other words, the i
-th row starts exactly with ri full cells. Similarly, the j-th column starts exactly with cjfull cells.
These are the r
and c values of some 3×4grid. Black cells are full and white cells are empty.
You have values of r
and c. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of r and c. Since the answer can be very large, find the answer modulo 1000000007(109+7). In other words, find the remainder after division of the answer by 1000000007(109+7).
Input
The first line contains two integers h
and w (1≤h,w≤103) — the height and width of the grid.
The second line contains h
integers r1,r2,…,rh (0≤ri≤w) — the values of r.
The third line contains w
integers c1,c2,…,cw (0≤cj≤h) — the values of c.
Output
Print the answer modulo 1000000007(109+7)
.
Examples
Input(copy)
3 4 0 3 1 0 2 3 0
Output(copy)
2
Input(copy)
1 1 0 1
Output(copy)
0
Input(copy)
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(copy)
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
, cvalues.
In the third example, make sure to print answer modulo (109+7)
-
思路:先按照他给出的行列信息给每个格子编号,例如每行r[i],给第i行的0-a[i]列格子标1,意思是这地方有黑格子,给第a[i]+1标2,意思这地方有白格子,在标的过程中,如果发现冲突,输出0,标完后,看值为0的格子有p个的话,答案就是2^p%mod
-
#include <iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; #define maxn 1050 #define mod 1000000007 #define ll long long ll mp[maxn][maxn]; int main() { ll h,w; scanf("%lld %lld",&h,&w); ll flag=0; for(ll i=1;i<=h;i++) { ll x; scanf("%lld",&x); for(ll j=1;j<=x;j++) { mp[i][j]=1; } mp[i][x+1]=2; //printf("x=%lld\n",x); } for(ll j=1;j<=w;j++) { ll y; scanf("%lld",&y); if(y!=h&&mp[y+1][j]==1) { /*printf("j=%lld y=%lld\n",j,y); for(ll n=1;n<=h;n++) { for(ll m=1;m<=w;m++) { printf("mp[%lld][%lld]=%lld ",n,m,mp[n][m]); } printf("\n"); }*/ flag=1; break; } mp[y+1][j]=2; for(ll i=1;i<=y;i++) { if(mp[i][j]==2) { flag=1; break; } mp[i][j]=1; } } if(flag==1) { printf("0\n"); return 0; } ll sum=1; for(ll i=1;i<=h;i++) { for(ll j=1;j<=w;j++) { if(mp[i][j]==0) { sum*=2; sum%=mod; } } } printf("%lld\n",sum); }