結果

問題 No.226 0-1パズル
ユーザー  nemunemu nemunemu
提出日時 2023-10-15 10:32:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,774 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 71,300 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-15 10:32:16
合計ジャッジ時間 2,154 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// No.226 0-1パズル
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int MOD = 1'000'000'007;
typedef long long ll;

int power_mod(ll a, ll b, int mod = MOD) {
    ll ret = 1;
    while (b) {
        if (b & 1) {
            ret *= a;
            ret %= mod;
        }
        a *= a;
        a %= mod;
        b = b >> 1;
    }
    return ret;
}

int count_mod(const vector<string> &S, int mod = MOD) {
    int m = 0;
    for (int j = 0; j < (int)S[0].size(); ++j) {
        char cand = '?';
        for (int i = 0; i < (int)S.size(); ++i) {
            if (S[i][j] == '?') continue;
            char should = (S[i][j] - '0') ^ (i % 2) + '0';
            if (cand != '?' && cand != should) {
                return 0;
            }
            cand = should;
        }
        if (cand == '?') ++m;
    }
    return power_mod(2, m);
}

int count2(const vector<string> &S) {
    int ret[2] = { 1, 1 };
    for (int i = 0; i < (int)S.size(); ++i) {
        for (int j = 0; j < (int)S[0].size(); ++j) {
            for (int val = 0; val < 2; ++val) {
                if (S[i][j] == '0' && (i + j + val) % 2 == 1) {
                    ret[val] = 0;
                }
                if (S[i][j] == '1' && (i + j + val) % 2 == 0) {
                    ret[val] = 0;
                }
            }
        }
    }
    return ret[0] + ret[1];
}

int main() {
    int H, W;
    cin >> H >> W;
    vector<string>S(H);
    for (int i = 0; i < H; ++i) cin >> S[i];

    ll res = count_mod(S);
    
    vector<string> S_t(W, string(H, '?'));
    for (int i = 0; i < W; ++i) {
        for (int j = 0; j < H; ++j) {
            S_t[i][j] = S[j][i];
        }
    }
    res += count_mod(S_t);

    res -= count2(S);

    cout << res % MOD << endl;
}
0