結果

問題 No.226 0-1パズル
ユーザー nebukuro09nebukuro09
提出日時 2018-12-10 18:00:47
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,606 bytes
コンパイル時間 741 ms
コンパイル使用メモリ 106,580 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-04 12:07:47
合計ジャッジ時間 1,719 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto A = H.iota.map!(_ => readln.chomp).array;
    long ans = 0;

    // 交互パターン
    {
        long tmp = 1;
        foreach (i; 0..H) {
            int zero = -1;
            foreach (j; 0..W) {
                if (A[i][j] == '0' && zero == -1) {
                    zero = j % 2;
                } else if (A[i][j] == '0' && zero != j % 2) {
                    tmp = 0;
                    break;
                } else if (A[i][j] == '1' && zero == -1) {
                    zero = 1 - j % 2;
                } else if (A[i][j] == '1' && zero != 1 - j % 2) {
                    tmp = 0;
                    break;
                }
            }
            if (zero == -1) {
                tmp = tmp * 2 % MOD;
            }
        }
        ans = (ans + tmp) % MOD;
    }

    // どこかに2連続があるパターン
    foreach (jj; 0..W-1) {
        foreach (k; 0..2) {
            long tmp = 1;
            auto row = new int[][](2, W);
            foreach (i; 0..2) fill(row[i], -1);
            foreach (jjj; 0..jj) row[0][jjj] = k ^ ((jj-jjj)%2);
            foreach (jjj; 0..jj) row[1][jjj] = row[0][jjj] ^ 1;
            row[0][jj] = row[0][jj+1] = k;
            row[1][jj] = row[1][jj+1] = k ^ 1;
            bool turn = false;
            outer: foreach (i; 0..H) {
                foreach (j; 0..W) {
                    if (A[i][j] == '?') continue;
                    bool b = A[i][j] == '1';
                    if (row[turn][j] != -1 && row[turn][j] != b) {
                        tmp = 0;
                        break outer;
                    }
                    if (row[turn^1][j] != -1 && row[turn^1][j] == b) {
                        tmp = 0;
                        break outer;
                    }
                    row[turn][j] = b;
                    row[turn^1][j] = b^1;
                }
                turn ^= 1;
            }
            if (tmp == 0) {
                continue;
            }
            long cnt = row[0].map!(x => x == -1).sum;
            ans = (ans + powmod(2, cnt, MOD)) % MOD;
        }
    }

    ans.writeln;
}


long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0