結果

問題 No.1141 田グリッド
ユーザー pmpm
提出日時 2020-07-31 22:44:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,090 bytes
コンパイル時間 1,935 ms
コンパイル使用メモリ 180,464 KB
実行使用メモリ 4,524 KB
最終ジャッジ日時 2023-09-21 01:19:20
合計ジャッジ時間 8,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 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,380 KB
testcase_13 AC 1,983 ms
4,524 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

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));
    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++;
            }
        }
    }

    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 = 0;
        rep(i, h)if(grid[i][c]==0)cnt++;
        rep(j, w)if(j!=c && grid[r][j]==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