結果

問題 No.1141 田グリッド
ユーザー firiexpfiriexp
提出日時 2020-07-31 21:52:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 3,269 bytes
コンパイル時間 1,144 ms
コンパイル使用メモリ 109,860 KB
実行使用メモリ 8,520 KB
最終ジャッジ日時 2023-09-20 23:00:42
合計ジャッジ時間 3,944 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 61 ms
4,380 KB
testcase_14 AC 74 ms
8,520 KB
testcase_15 AC 60 ms
4,376 KB
testcase_16 AC 59 ms
4,384 KB
testcase_17 AC 59 ms
4,380 KB
testcase_18 AC 31 ms
4,548 KB
testcase_19 AC 32 ms
4,376 KB
testcase_20 AC 31 ms
4,380 KB
testcase_21 AC 60 ms
4,376 KB
testcase_22 AC 60 ms
4,400 KB
testcase_23 AC 60 ms
4,380 KB
testcase_24 AC 51 ms
4,404 KB
testcase_25 AC 51 ms
4,376 KB
testcase_26 AC 51 ms
4,380 KB
testcase_27 AC 51 ms
4,376 KB
testcase_28 AC 51 ms
4,380 KB
testcase_29 AC 44 ms
4,380 KB
testcase_30 AC 44 ms
4,380 KB
testcase_31 AC 37 ms
4,500 KB
testcase_32 AC 49 ms
4,376 KB
testcase_33 AC 50 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<u32 M = 1000000007>
struct modint{
    u32 val;
    modint(): val(0){}
    template<typename T>
    modint(T t){t %= (T)M; if(t < 0) t += (T)M; val = t;}

    modint pow(ll k) const {
        modint res(1), x(val);
        while(k){
            if(k&1) res *= x;
            x *= x;
            k >>= 1;
        }
        return res;
    }
    template<typename T>
    modint& operator=(T t){t %= (T)M; if(t < 0) t += (T)M; val = t; return *this;}
    modint inv() const {return pow(M-2);}
    modint& operator+=(modint a){val += a.val; if(val >= M) val -= M; return *this;}
    modint& operator-=(modint a){if(val < a.val) val += M-a.val; else val -= a.val; return *this;}
    modint& operator*=(modint a){val = (u64)val*a.val%M; return *this;}
    modint& operator/=(modint a){return (*this) *= a.inv();}
    modint operator+(modint a) const {return modint(val) +=a;}
    modint operator-(modint a) const {return modint(val) -=a;}
    modint operator*(modint a) const {return modint(val) *=a;}
    modint operator/(modint a) const {return modint(val) /=a;}
    modint operator-(){return modint(M-val);}
    bool operator==(const modint a) const {return val == a.val;}
    bool operator!=(const modint a) const {return val != a.val;}
    bool operator<(const modint a) const {return val < a.val;}
};
using mint = modint<MOD>;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int h, w;
    cin >> h >> w;
    auto v = make_v(h+1, w+1, mint(1));
    auto z = make_v(h+1, w+1, 0);
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            int x;
            scanf("%d", &x);
            if(x) v[i+1][j+1] = x;
            else {
                v[i+1][j+1] = 1;
                z[i+1][j+1] = 1;
            }
        }
    }
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j <= w; ++j) {
            v[i+1][j] *= v[i][j];
            z[i+1][j] += z[i][j];
        }
    }
    for (int i = 0; i <= h; ++i) {
        for (int j = 0; j < w; ++j) {
            v[i][j+1] *= v[i][j];
            z[i][j+1] += z[i][j];
        }
    }
    auto f = [&](int x1, int y1, int x2, int y2) -> mint {
        if(z[x2][y2] - z[x1][y2] - z[x2][y1] + z[x1][y1]) return 0;
        else return v[x2][y2]*v[x1][y1]/(v[x1][y2]*v[x2][y1]);
    };
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        cout << (f(0, 0, a, b)*f(a+1, 0, h, b)*f(0, b+1, a, w)*f(a+1, b+1, h, w)).val << "\n";
    }
    return 0;
}
0