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

题目链接:Filling the Grid

题意:

题意很明确了,题目要求在网格满足一定条件下有多少种可能。
因为输入rj时,第j行的前rj+1个网格是确定的(前rj个是黑色,第rj+1个是白色)。
那么我们只要枚举每个位置是否同时在rj+1和ci+1之外。
找出这些位置的个数,因为这些位置可以为白或黑。
满足条件的方案数就是pow(2,sum)。
时间复杂度是O(hw)的。

首先处理每一行,按照要求把把相应的格子涂成黑色(标记为2),并且在该行最后一个黑色个字后边的那个格子上标记为1,表示到这里结束;
然后处理每一列;这里需要注意一个坑点;当处理每一列时,如果对当前格子进行标号时,与之前的标号冲出,说明出现矛盾的情况,这是满足的方案数应该为0;

AC代码:

By hpuxiaoxiaoqi, contest: Codeforces Round #589 (Div. 2), problem: (B) Filling the Grid, Accepted, #
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
const int maxx=1e3+7;
const int mod=1000000007;
int r[maxx],c[maxx];
int mpp[maxx][maxx];
ll Prowermod(ll a,ll n,int b)
{
    ll ans=1;
    a=a%b;
    while(n)
    {
        if(n&1) ans=(ans*a)%b;
        n/=2;
        a=(a*a)%b;
    }
    return ans;
}
int main()
{
    int h,w;
    cin >>h>>w;
    int flag=0;
    for(int i=0;i<h;i++) cin >>r[i];
    for(int i=0;i<w;i++) cin >>c[i];
    for(int i=0;i<h;i++)
    {
        if(r[i]==0) mpp[i][0]=1;
        if(r[i])
        {
            for(int j=0;j<r[i];j++)
            {
                mpp[i][j]=2;
            }
            if(r[i]!=w) mpp[i][r[i]]=1;
        }
    }
    for(int j=0;j<w;j++)
    {
        if(c[j]==0)
        {
            if( mpp[0][j]==2)
            {
                flag=1;
                break;
            }
            else mpp[0][j]=1;
        }
 
        if(c[j])
        {
            for(int i=0;i<c[j];i++)
            {
                if(mpp[i][j]==1)
                {
                    flag=1;
                    break;
                }
                else mpp[i][j]=2;
            }
            if(c[j]!=h)
            {
                if(mpp[c[j]][j]==2)
                {
                    flag=1;
                    break;
                }
                else mpp[c[j]][j]=1;
            }
        }
    }
    int sum=0;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(mpp[i][j]==0) sum++;
        }
    }
    ll ans=Prowermod(2,sum,mod);
    if(flag) ans=0;
    cout <<ans<<endl;
    return 0;
}

题目测试数据如下:
Test: #1, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
3 4
0 3 1
0 2 3 0
Output
2
Answer
2
Checker Log
ok 1 number(s): “2”
Test: #2, time: 15 ms., memory: 3972 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1
0
1
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #3, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
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
Answer
797922655
Checker Log
ok 1 number(s): “797922655”
Test: #4, time: 15 ms., memory: 3948 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
4 4
2 0 3 1
1 3 0 3
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #5, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
3 6
0 0 0
0 0 0 0 0 0
Output
1024
Answer
1024
Checker Log
ok 1 number(s): “1024”
Test: #6, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1000 1000
1000 1000 403 147 415 210 308 153 242 214 262 83 143 264 247 298 257 44 304 160 207 180 143 364 186 191 48 108 95 74 89 218 86 54 134 95 200 135 206 132 141 84 156 141 71 141 94 47 67 68 138 30 46 75 34 314 105 66 80 164 31 7 51 90 146 158 122 118 52 88 45 18 129 191 109 165 81 143 71 97 48 81 110 12 70 31 109 59 63 71 47 74 135 80 85 142 84 163 48 146 8 98 51 91 106 98 77 101 137 49 76 82 43 24 68 73 91 88 84 53 45 19 105 74 104 86 89 69 34 55 81 10 58 33 150 105 37 46 47 90 72 64 27 66 30 110 …
Output
863607944
Answer
863607944
Checker Log
ok 1 number(s): “863607944”
Test: #7, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
4 5
2 5 2 2
4 4 0 2 2
Output
4
Answer
4
Checker Log
ok 1 number(s): “4”
Test: #8, time: 31 ms., memory: 3940 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
877 858
617 617 847 617 376 617 858 617 847 724 376 172 172 57 172 402 172 798 376 402 774 376 376 376 557 376 724 434 847 87 376 172 774 777 636 724 636 587 479 798 172 479 57 539 514 672 557 300 557 16 798 499 300 587 613 195 804 49 402 113 479 49 204 613 672 9 172 172 508 400 280 320 847 546 376 858 567 280 645 400 514 87 366 724 798 335 847 724 172 607 113 248 75 826 77 788 732 581 770 154 57 777 607 113 280 479 557 154 248 844 734 514 652 826 280 668 479 574 452 479 656 321 508 248 836 366 280 300 28…
Output
494501718
Answer
494501718
Checker Log
ok 1 number(s): “494501718”
Test: #9, time: 30 ms., memory: 3952 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
799 557
557 557 165 154 252 157 189 31 145 146 280 115 384 6 146 124 218 138 129 107 160 242 197 166 88 36 148 33 9 109 101 90 86 41 134 107 14 123 160 47 158 34 91 88 83 103 77 112 40 54 62 46 103 60 96 28 70 86 98 42 72 52 48 161 17 68 42 76 23 61 22 15 91 82 61 51 66 96 99 28 14 5 16 24 26 66 94 64 123 25 52 88 34 67 113 39 26 76 41 71 58 112 11 53 67 34 51 53 26 22 92 28 68 58 58 151 17 59 16 35 66 48 90 33 24 55 29 67 5 11 34 26 17 47 38 20 33 42 37 38 44 22 67 41 45 9 80 57 94 44 37 104 33 31 60 31 …
Output
465261332
Answer
465261332
Checker Log
ok 1 number(s): “465261332”
Test: #10, time: 15 ms., memory: 3952 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1000 900
166 262 897 32 398 317 587 39 17 169 87 431 586 372 701 332 711 516 437 743 233 207 436 554 7 362 522 781 556 727 609 277 255 734 209 376 527 304 658 507 46 663 392 387 890 271 606 138 47 539 240 580 508 599 221 818 634 556 383 278 896 458 766 65 256 851 190 13 127 502 859 673 131 680 216 854 848 262 285 296 12 842 78 216 302 257 535 733 525 385 868 76 8 730 43 457 712 623 196 842 598 19 466 472 272 843 660 154 796 547 350 3 76 530 286 691 31 258 692 437 210 645 827 319 356 434 311 490 592 265 13…
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #11, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
Output
1
Answer
1
Checker Log
ok 1 number(s): “1”
Test: #12, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
501 783
621 157 783 621 438 621 548 446 711 711 548 438 2 157 314 621 754 438 548 711 314 407 82 571 608 608 211 42 541 672 608 120 541 672 407 438 64 326 541 326 288 211 130 711 38 54 541 2 54 223 541 646 288 384 384 326 423 157 38 541 194 724 732 711 512 783 38 783 384 638 438 57 500 541 754 423 276 464 276 421 253 696 783 219 555 211 130 181 655 253 336 76 571 618 181 429 229 544 618 459 336 680 732 421 637 38 253 421 311 421 288 541 686 782 680 223 504 38 519 583 110 19 273 459 38 157 637 637 211 157 …
Output
568124848
Answer
568124848
Checker Log
ok 1 number(s): “568124848”
Test: #13, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
988 611
611 226 465 272 462 611 188 281 211 69 198 34 209 187 232 63 210 165 117 108 106 285 149 221 80 99 197 41 179 125 184 90 229 101 100 169 137 125 194 176 30 13 205 48 41 55 173 37 265 131 84 86 189 108 105 59 76 210 47 39 39 107 78 99 13 66 91 33 67 138 6 64 121 74 70 84 101 66 88 52 66 115 69 115 55 120 43 43 15 80 142 31 60 70 182 94 69 41 57 52 45 33 22 59 61 86 36 30 47 110 34 34 62 122 25 74 85 119 81 78 85 31 29 36 63 98 59 60 116 93 3 4 24 75 21 55 53 63 74 29 69 78 80 15 52 72 156 18 46 60 …
Output
420636125
Answer
420636125
Checker Log
ok 1 number(s): “420636125”
Test: #14, time: 15 ms., memory: 3972 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
626 541
388 354 164 271 90 29 531 61 50 277 1 478 81 471 457 23 69 80 401 493 21 486 128 465 466 68 286 531 425 365 335 97 33 93 182 23 523 250 401 474 14 374 166 259 448 145 261 304 442 401 108 175 17 262 36 21 441 2 202 51 389 381 136 69 491 325 76 10 196 441 460 350 241 284 438 485 497 126 393 174 307 63 1 396 295 530 325 505 517 500 461 97 336 400 250 263 406 376 151 117 33 228 223 50 69 377 23 228 306 159 82 83 532 310 220 458 100 74 288 437 304 529 177 436 486 10 292 476 128 43 531 336 118 12 333 41…
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #15, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
719 791
341 702 341 702 702 163 341 341 604 791 702 390 702 702 702 591 341 779 591 591 16 372 372 67 233 702 331 713 604 443 159 124 591 719 647 779 443 786 632 372 295 48 295 618 159 591 517 390 713 517 223 551 223 700 198 700 443 698 295 663 551 551 458 591 499 372 645 632 647 48 591 91 499 198 632 645 779 209 791 66 284 48 295 323 746 669 551 153 632 284 16 597 323 499 295 27 712 542 251 124 264 124 746 754 654 639 6 697 163 198 251 251 314 180 458 697 435 681 123 765 761 719 372 497 746 748 171 27 31…
Output
974118739
Answer
974118739
Checker Log
ok 1 number(s): “974118739”
Test: #16, time: 31 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
705 618
618 618 538 145 34 190 392 51 87 424 235 312 200 162 42 273 46 185 208 115 194 123 117 45 87 174 38 89 63 52 43 106 23 134 128 12 49 139 55 42 44 23 98 23 28 39 118 33 79 53 91 69 43 106 44 139 40 42 85 6 50 29 52 88 27 62 51 35 102 42 52 72 29 74 36 33 73 44 75 79 55 31 38 44 26 62 88 53 44 61 32 66 62 60 33 47 48 70 36 39 19 51 38 95 23 44 26 51 43 40 73 57 49 68 49 62 115 37 80 53 44 59 109 10 57 46 29 115 74 74 60 106 24 7 16 86 54 70 32 59 23 86 61 49 20 94 61 36 145 56 18 39 17 16 63 48 43 1…
Output
422994246
Answer
422994246
Checker Log
ok 1 number(s): “422994246”
Test: #17, time: 31 ms., memory: 3984 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
837 541
434 335 339 332 345 113 214 439 158 25 457 307 106 122 61 487 333 99 525 349 430 250 109 257 497 221 197 537 471 453 524 385 212 319 538 184 263 316 200 203 459 387 37 246 324 151 295 351 386 389 279 217 265 53 19 440 512 170 293 134 120 165 413 279 208 353 265 474 453 391 293 417 402 234 164 204 44 204 473 422 375 181 109 256 159 154 426 480 502 482 222 280 489 524 168 338 322 381 154 251 324 449 383 494 116 354 476 249 226 392 116 232 162 69 196 368 292 302 303 55 41 348 451 378 102 252 464 362 …
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #18, time: 30 ms., memory: 3972 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
930 565
565 153 365 365 365 89 365 365 89 365 460 135 565 537 537 365 365 537 565 135 537 317 459 135 317 317 317 537 557 209 512 317 317 537 21 70 209 153 276 70 501 459 557 309 557 537 501 478 276 459 29 501 360 209 459 70 459 557 537 478 135 135 276 309 276 134 209 228 29 459 21 557 276 97 392 21 392 87 134 209 501 459 512 153 134 342 29 87 526 309 193 557 459 526 87 501 193 360 516 157 193 193 58 478 58 193 228 373 134 47 21 459 193 317 21 408 47 516 565 454 501 557 87 47 134 317 213 360 71 512 153 45…
Output
265940745
Answer
265940745
Checker Log
ok 1 number(s): “265940745”
Test: #19, time: 15 ms., memory: 3944 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
923 619
619 619 320 114 201 241 545 191 225 196 429 150 220 166 131 232 292 286 255 146 151 134 78 142 200 77 109 144 41 111 113 207 197 202 110 116 216 41 89 87 43 88 117 122 76 175 93 40 30 193 30 21 96 153 116 102 137 48 53 138 74 160 128 104 70 18 105 52 176 7 33 45 19 76 98 14 189 42 20 76 39 27 110 149 102 53 58 9 54 98 101 117 63 113 123 87 71 59 65 110 77 138 49 34 149 58 71 66 25 114 84 86 68 50 77 18 46 19 24 114 6 52 19 48 36 83 15 28 40 62 98 53 16 38 63 68 58 13 19 35 52 74 59 88 99 92 39 73 …
Output
509184234
Answer
509184234
Checker Log
ok 1 number(s): “509184234”
Test: #20, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
554 542
438 25 176 32 171 433 210 398 288 86 258 316 456 500 374 449 509 393 498 466 244 184 273 134 535 325 224 396 117 128 400 9 390 74 424 425 384 314 24 468 336 8 333 497 320 156 255 412 205 410 379 343 137 351 218 302 523 229 425 229 271 203 95 338 52 20 520 355 353 349 216 129 257 262 49 115 218 123 67 143 366 514 483 382 519 108 328 82 408 523 6 126 50 384 493 534 397 216 21 66 242 6 157 1 28 81 224 266 18 338 203 378 26 517 420 525 522 177 243 354 341 218 64 457 71 229 379 167 388 298 54 529 249 2…
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #21, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
4 4
3 0 0 0
0 0 0 0
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #22, time: 30 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
3 4
4 4 4
0 3 3 3
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #23, time: 30 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 2
1 1
1 0
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #24, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
3 5
4 3 2
3 2 2 1 0
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #25, time: 31 ms., memory: 3988 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 2
1 1
0 0
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #26, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
3 3
3 3 3
0 0 0
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #27, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
4 4
4 4 3 3
4 4 4 1
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #28, time: 15 ms., memory: 3980 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 4
2 2
0 0 0 2
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #29, time: 15 ms., memory: 3972 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1 1
1
0
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”
Test: #30, time: 15 ms., memory: 3976 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2 2
2 1
1 1
Output
0
Answer
0
Checker Log
ok 1 number(s): “0”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值