結果

問題 No.1141 田グリッド
ユーザー kyort0nkyort0n
提出日時 2020-07-31 21:41:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 2,457 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 173,728 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2023-09-20 22:23:32
合計ジャッジ時間 5,879 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,388 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,384 KB
testcase_13 AC 110 ms
4,632 KB
testcase_14 AC 119 ms
6,528 KB
testcase_15 AC 107 ms
4,380 KB
testcase_16 AC 108 ms
4,380 KB
testcase_17 AC 106 ms
4,380 KB
testcase_18 AC 105 ms
4,384 KB
testcase_19 AC 106 ms
4,380 KB
testcase_20 AC 105 ms
4,380 KB
testcase_21 AC 106 ms
4,384 KB
testcase_22 AC 105 ms
4,384 KB
testcase_23 AC 106 ms
4,380 KB
testcase_24 AC 107 ms
4,384 KB
testcase_25 AC 105 ms
4,380 KB
testcase_26 AC 106 ms
4,380 KB
testcase_27 AC 105 ms
4,384 KB
testcase_28 AC 107 ms
4,380 KB
testcase_29 AC 105 ms
4,384 KB
testcase_30 AC 104 ms
4,380 KB
testcase_31 AC 105 ms
4,384 KB
testcase_32 AC 104 ms
4,384 KB
testcase_33 AC 105 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
const ll mod = 1000000007;
ll H, W, Q;
vector<ll> inv, FactorialInv, Factorial;
ll beki(ll a, ll b){
    ll ret = 1 % mod;
    a %= mod;
    while(b) {
        if(b & 1LL) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}
void init_combination(ll MAX){
    Factorial.resize(MAX + 1);
    FactorialInv.resize(MAX + 1);
    inv.resize(MAX + 1);
    Factorial[0] = 1;
    inv[0] = 1;
    for(int i = 1; i <= MAX; i++){
        Factorial[i] = Factorial[i - 1] * i % mod;
    }
    FactorialInv[MAX] = beki(Factorial[MAX], mod - 2);
    for(ll i = MAX - 1; i >= 0; i--) {
        FactorialInv[i] = FactorialInv[i+1] * (i+1) % mod;
    }
    for(int i = 1; i <= MAX; i++) {
        inv[i] = FactorialInv[i] * Factorial[i-1] % mod;
    }
}
ll combination(ll a, ll b){
    if((a == b) || (b == 0)){
        return 1;
    }
    if(a < b) return 0;
    if(b < 0) return 0;
    ll ans = Factorial[a] * FactorialInv[b] % mod;
    ans = ans * FactorialInv[a - b] % mod;
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> H >> W;
    vector<vector<ll>> A(H, vector<ll>(W));
    vector<ll> row(H, 1), column(W, 1), zerorow(H, 0), zerocolumn(W, 0);
    ll prod = 1;
    ll zero = 0;
    for(int h = 0; h < H; h++) {
        for(int w = 0; w < W; w++) {
            cin >> A[h][w];
            if(A[h][w]) row[h] = row[h] * A[h][w] % mod;
            else zerorow[h]++;
            if(A[h][w]) column[w] = column[w] * A[h][w] % mod;
            else zerocolumn[w]++;
            if(A[h][w]) prod = prod * A[h][w] % mod;
            else zero++;
        }
    }
    cin >> Q;
    while(Q--) {
        int h, w;
        cin >> h >> w;
        h--;
        w--;
        ll ans = prod;
        ans = ans * beki(row[h], mod - 2) % mod;
        ans = ans * beki(column[w], mod - 2) % mod;
        if(A[h][w]) ans = ans * A[h][w] % mod;
        ll tmp = zerorow[h] + zerocolumn[w];
        if(A[h][w] == 0) tmp--;
        if(tmp != zero) ans = 0;
        cout << ans << endl;
    }
    return 0;
}
0