結果

問題 No.1141 田グリッド
ユーザー yakkiyakki
提出日時 2020-07-31 22:30:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,900 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 128,664 KB
実行使用メモリ 8,728 KB
最終ジャッジ日時 2023-09-21 00:54:04
合計ジャッジ時間 7,740 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 211 ms
4,912 KB
testcase_14 AC 236 ms
8,728 KB
testcase_15 AC 226 ms
4,576 KB
testcase_16 AC 227 ms
4,716 KB
testcase_17 AC 227 ms
4,612 KB
testcase_18 AC 225 ms
4,796 KB
testcase_19 AC 224 ms
4,592 KB
testcase_20 AC 222 ms
4,612 KB
testcase_21 AC 226 ms
4,556 KB
testcase_22 AC 227 ms
4,548 KB
testcase_23 AC 223 ms
4,624 KB
testcase_24 AC 226 ms
4,760 KB
testcase_25 AC 225 ms
4,380 KB
testcase_26 AC 227 ms
4,608 KB
testcase_27 AC 226 ms
4,688 KB
testcase_28 AC 224 ms
4,380 KB
testcase_29 AC 225 ms
4,548 KB
testcase_30 AC 223 ms
4,664 KB
testcase_31 AC 225 ms
4,608 KB
testcase_32 AC 225 ms
4,552 KB
testcase_33 AC 226 ms
4,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
using namespace std;
 
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1)
 
const double EPS = 1e-10;
 
using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;

ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll 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<int>> a(h,vector<int>(w));
    rep(i,h)rep(j,w) cin >> a[i][j];
    int q; cin >> q;
    vector<vector<ll>> seki(h+1,vector<ll>(w+1));
    rep(i,h+1) seki[i][0] = 1;
    rep(i,w+1) seki[0][i] = 1;
    rep(i,h)rep(j,w){
        seki[i+1][j+1] = (a[i][j] == 0 ? 1:a[i][j])*seki[i+1][j]%MOD*seki[i][j+1]%MOD*modinv(seki[i][j],MOD)%MOD;
    }
    vector<int> h0(h),w0(w);
    int cnt = 0;
    rep(i,h){
        rep(j,w){
            if(a[i][j] == 0){
                h0[i]++;
                cnt++;
            }
        }
    }
    rep(i,w){
        rep(j,h){
            if(a[j][i] == 0){
                w0[i]++;
            }
        }
    }
    while(q--){
        int r,c; cin >> r >> c;
        r--; c--;
        ll ans = seki[h][w]*modinv(seki[r+1][w]*modinv(seki[r][w],MOD)%MOD,MOD)%MOD*modinv(seki[h][c+1]*modinv(seki[h][c]%MOD,MOD),MOD)%MOD*(a[r][c] == 0?1:a[r][c])%MOD;
        if(cnt-h0[r]-w0[c]+(a[r][c] == 0) > 0) cout << 0 << endl;
        else cout << ans << endl;
    }
}
0