結果

問題 No.1141 田グリッド
ユーザー pmpm
提出日時 2020-07-31 22:46:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,159 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 181,096 KB
実行使用メモリ 6,924 KB
最終ジャッジ日時 2023-09-21 01:23:07
合計ジャッジ時間 5,373 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 85 ms
5,004 KB
testcase_14 AC 91 ms
6,924 KB
testcase_15 AC 64 ms
4,412 KB
testcase_16 AC 64 ms
4,444 KB
testcase_17 AC 64 ms
4,380 KB
testcase_18 AC 63 ms
4,412 KB
testcase_19 AC 63 ms
4,384 KB
testcase_20 AC 63 ms
4,380 KB
testcase_21 AC 65 ms
4,412 KB
testcase_22 AC 64 ms
4,376 KB
testcase_23 AC 65 ms
4,444 KB
testcase_24 AC 63 ms
4,376 KB
testcase_25 AC 64 ms
4,376 KB
testcase_26 AC 63 ms
4,376 KB
testcase_27 AC 63 ms
4,380 KB
testcase_28 AC 64 ms
4,380 KB
testcase_29 AC 63 ms
4,376 KB
testcase_30 AC 63 ms
4,380 KB
testcase_31 AC 63 ms
4,376 KB
testcase_32 AC 64 ms
4,380 KB
testcase_33 AC 63 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>; 
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main(){
    int h, w; cin >> h >> w;
    vector<vector<ll>> grid(h, vector<ll>(w));
    vector<ll> countZ_h(h), countZ_w(w);
    ll sum = 1, zero = 0;
    rep(i, h){
        rep(j, w){
            cin >> grid[i][j];
            if(grid[i][j])
            sum = (sum * grid[i][j]) % MOD;
            else{
                zero++;
                countZ_h[i]++;
                countZ_w[j]++;
            }
        }
    }

    vector<ll> H(h), W(w);
    rep(i, h){
        ll num = 1;
        rep(j, w){
            if(grid[i][j])
            num = (num * grid[i][j]) % MOD;
        }
        H[i] = modinv(num, MOD);
    }
    rep(j, w){
        ll num = 1;
        rep(i, h){
            if(grid[i][j])
            num = (num * grid[i][j]) % MOD;
        }
        W[j] = modinv(num, MOD);
    }
    
    int Q; cin >> Q;
    vector<ll> ans(Q);
    rep(q, Q){
        ll r, c; cin >> r >> c; r--, c--;
        ll cnt = countZ_h[r] + countZ_w[c];
        if(grid[r][c]==0)cnt--;
        if(cnt < zero)ans[q] = 0;
        else{
            ll res = sum;
            res = (res * H[r]) % MOD;
            res = (res * W[c]) % MOD;
            if(grid[r][c])res = (res * grid[r][c]) % MOD;
            ans[q] = res;
        }
    }
    for(auto a: ans)cout << a << ln;
}
    
0