結果

問題 No.1141 田グリッド
ユーザー lorent_kyoprolorent_kyopro
提出日時 2020-08-01 04:32:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 218 ms / 2,000 ms
コード長 3,292 bytes
コンパイル時間 2,332 ms
コンパイル使用メモリ 214,096 KB
実行使用メモリ 19,368 KB
最終ジャッジ日時 2023-09-21 10:18:48
合計ジャッジ時間 9,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 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 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 188 ms
9,312 KB
testcase_14 AC 218 ms
19,368 KB
testcase_15 AC 185 ms
7,528 KB
testcase_16 AC 187 ms
7,436 KB
testcase_17 AC 186 ms
7,416 KB
testcase_18 AC 103 ms
7,536 KB
testcase_19 AC 103 ms
7,492 KB
testcase_20 AC 103 ms
7,588 KB
testcase_21 AC 185 ms
7,476 KB
testcase_22 AC 186 ms
7,416 KB
testcase_23 AC 186 ms
7,496 KB
testcase_24 AC 164 ms
7,512 KB
testcase_25 AC 164 ms
7,528 KB
testcase_26 AC 164 ms
7,436 KB
testcase_27 AC 165 ms
7,588 KB
testcase_28 AC 165 ms
7,588 KB
testcase_29 AC 140 ms
7,532 KB
testcase_30 AC 142 ms
7,492 KB
testcase_31 AC 117 ms
7,488 KB
testcase_32 AC 159 ms
7,576 KB
testcase_33 AC 155 ms
7,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define rrep(i,n) for(int i=(int)n-1;i>=0;--i)
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T& a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U& u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U& u,const V... v){for(auto& e:u)fill_v<T>(e,v...);}

const int mod = 1000000007;

struct mint {
    long long x;
    mint(long long x=0) : x((x % mod + mod) % mod) {}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int h, w;
    cin >> h >> w;
    auto a = make_vec<int>(h, w);
    rep(i, h) rep(j, w) cin >> a[i][j];
    
    auto sum = make_vec<mint>(4, h+1, w+1);
    fill_v<mint>(sum, 1);

    rep(i, h) rep(j, w) {
        if (sum[0][i][j].x == 0) sum[0][i+1][j+1] = 0;
        else sum[0][i+1][j+1] = sum[0][i][j+1] * sum[0][i+1][j] * a[i][j] / sum[0][i][j];
    }
    rep(i, h) rrep(j, w) {
        if (sum[1][i][j+1].x == 0) sum[1][i+1][j] = 0;
        else sum[1][i+1][j] = sum[1][i][j] * sum[1][i+1][j+1] * a[i][j] / sum[1][i][j+1];
    }
    rrep(i, h) rep(j, w) {
        if (sum[2][i+1][j].x == 0) sum[2][i][j+1] = 0;
        else sum[2][i][j+1] = sum[2][i][j] * sum[2][i+1][j+1] * a[i][j] / sum[2][i+1][j];
    }
    rrep(i, h) rrep(j, w) {
        if (sum[3][i+1][j+1].x == 0) sum[3][i][j] = 0;
        else sum[3][i][j] = sum[3][i+1][j] * sum[3][i][j+1] * a[i][j] / sum[3][i+1][j+1];
    }

    // rep(t, 4) {
    //     rep(i, h+1) rep(j, w+1) {
    //         cout << sum[t][i][j] << (j == w ? '\n' : ' ');
    //     }
    //     cout << endl;
    // }
    
    int q;
    cin >> q;
    while (q--) {
        int r, c;
        cin >> r >> c;
        r--; c--;
        mint ans = sum[0][r][c] * sum[1][r][c+1] * sum[2][r+1][c] * sum[3][r+1][c+1];
        cout << ans << endl;
    }
}
0