結果

問題 No.1141 田グリッド
ユーザー ぷらぷら
提出日時 2021-03-16 00:28:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,962 bytes
コンパイル時間 2,025 ms
コンパイル使用メモリ 170,144 KB
実行使用メモリ 6,784 KB
最終ジャッジ日時 2024-04-25 03:20:12
合計ジャッジ時間 7,355 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 144 ms
5,376 KB
testcase_14 AC 153 ms
6,784 KB
testcase_15 AC 141 ms
5,376 KB
testcase_16 AC 145 ms
5,376 KB
testcase_17 AC 146 ms
5,376 KB
testcase_18 AC 132 ms
5,376 KB
testcase_19 AC 131 ms
5,376 KB
testcase_20 AC 128 ms
5,376 KB
testcase_21 AC 144 ms
5,376 KB
testcase_22 AC 146 ms
5,376 KB
testcase_23 AC 145 ms
5,376 KB
testcase_24 AC 137 ms
5,376 KB
testcase_25 AC 136 ms
5,376 KB
testcase_26 AC 129 ms
5,376 KB
testcase_27 AC 131 ms
5,376 KB
testcase_28 AC 137 ms
5,376 KB
testcase_29 AC 130 ms
5,376 KB
testcase_30 AC 130 ms
5,376 KB
testcase_31 AC 134 ms
5,376 KB
testcase_32 AC 134 ms
5,376 KB
testcase_33 AC 131 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int mod = 1000000007;

inline long long modpow(long long a,long long b,long long m) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= m;
        }
        (a *= a) %= m;
        b /= 2;
    }
    return ans;
}

int main() {
    int H,W;
    cin >> H >> W;
    vector<vector<int>>A(H,vector<int>(W));
    long long sum = 1,sum2 = 0;
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            cin >> A[i][j];
            if(A[i][j]) {
                sum *= A[i][j];
                sum %= mod;
            }
            else {
                sum2++;
            }
        }
    }
    vector<long long>sumh(W),sumw(H),sumh2(W),sumw2(H);
    for(int i = 0; i < H; i++) {
        long long tmp = 1,tmp2 = 0;
        for(int j = 0; j < W; j++) {
            if(A[i][j]) {
                tmp *= A[i][j];
                tmp %= mod;
            }
            else {
                tmp2++;
            }
        }
        sumw[i] = tmp;
        sumw2[i] = tmp2;
    }
    for(int i = 0; i < W; i++) {
        long long tmp = 1,tmp2 = 0;
        for(int j = 0; j < H; j++) {
            if(A[j][i]) {
                tmp *= A[j][i];
                tmp %= mod;
            }
            else {
                tmp2++;
            }
        }
        sumh[i] = tmp;
        sumh2[i] = tmp2;
    }
    int Q;
    cin >> Q;
    while (Q--) {
        int r,c;
        cin >> r >> c;
        r--;c--;
        long long tmp = 1,tmp2 = 0;
        tmp *= sumw[r];
        tmp *= sumh[c];
        tmp %= mod;
        tmp2 += sumw2[r];
        tmp2 += sumh2[c];
        if(A[r][c]) {
            tmp *= modpow(A[r][c],mod-2,mod);
            tmp %= mod;
        }
        else {
            tmp2--;
        }
        if(sum2-tmp2) {
            cout << 0 << endl;
        }
        else {
            cout << sum*modpow(tmp,mod-2,mod)%mod << endl;
        }
    }
}
0