結果

問題 No.1141 田グリッド
ユーザー ShibuyapShibuyap
提出日時 2020-07-31 21:44:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 2,189 bytes
コンパイル時間 2,256 ms
コンパイル使用メモリ 208,328 KB
実行使用メモリ 5,800 KB
最終ジャッジ日時 2023-09-20 22:29:31
合計ジャッジ時間 7,351 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 171 ms
5,492 KB
testcase_14 AC 175 ms
5,800 KB
testcase_15 AC 167 ms
4,908 KB
testcase_16 AC 166 ms
4,892 KB
testcase_17 AC 165 ms
4,912 KB
testcase_18 AC 126 ms
4,900 KB
testcase_19 AC 126 ms
4,920 KB
testcase_20 AC 125 ms
4,924 KB
testcase_21 AC 167 ms
4,988 KB
testcase_22 AC 167 ms
4,972 KB
testcase_23 AC 167 ms
4,968 KB
testcase_24 AC 125 ms
4,980 KB
testcase_25 AC 127 ms
4,924 KB
testcase_26 AC 126 ms
5,080 KB
testcase_27 AC 125 ms
5,188 KB
testcase_28 AC 127 ms
5,008 KB
testcase_29 AC 128 ms
4,928 KB
testcase_30 AC 125 ms
5,008 KB
testcase_31 AC 130 ms
4,980 KB
testcase_32 AC 127 ms
4,920 KB
testcase_33 AC 124 ms
4,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("YES");}else{puts("NO");}
#define MAX_N 200005

// ax + by = gcd(a, b) となるような (x, y) を求める
// a と b は互いに素として ax + by = 1 となる (x, y) を求める
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x); // 再帰
    y -= a / b * x;
    return d;
}


// 負の数に対応した mod
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}


// 逆元計算 (a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m); // x % m だが、x が負かもしれないので
}

int main() {
    ll h, w;
    cin >> h >> w;
    ll a[h][w];
    int zh[h] = {};
    int zw[w] = {};
    int zero = 0;
    set<P> se;
    ll mul = 1;
    ll MOD = 1000000007;
    rep(i,h)rep(j,w){
        cin >> a[i][j];
        if(a[i][j] == 0){
            se.insert(P(i,j));
            zero++;
            zh[i]++;
            zw[j]++;
            a[i][j] = 1;
        }
        mul *= a[i][j];
        mul %= MOD;
    }
    
    ll Q;
    cin >> Q;
    ll r[Q], c[Q];
    rep(i,Q){
        cin >> r[i] >> c[i];
        r[i]--; c[i]--;
    }

    ll mh[h], mw[w];
    rep(i,h){
        mh[i] = 1;
        rep(j,w){
            mh[i] *= a[i][j];
            mh[i] %= MOD;
        }
    }
    rep(j,w){
        mw[j] = 1;
        rep(i,h){
            mw[j] *= a[i][j];
            mw[j] %= MOD;
        }
    }

    rep(i,Q){
        int tmp = zh[r[i]] + zw[c[i]];
        if(se.find(P(r[i],c[i])) != se.end()) tmp--;
        if(tmp < zero){
            cout << 0 << endl;
            continue;
        }
        ll ans = mul * modinv(mh[r[i]],MOD) % MOD * modinv(mw[c[i]],MOD) % MOD * a[r[i]][c[i]] % MOD;
        cout << ans << endl;
    }

    return 0;
}
 
 
0