結果

問題 No.1141 田グリッド
ユーザー firiexpfiriexp
提出日時 2020-07-31 21:52:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,269 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 112,084 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-07-06 17:43:51
合計ジャッジ時間 3,932 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 59 ms
6,944 KB
testcase_14 AC 69 ms
8,832 KB
testcase_15 AC 57 ms
6,944 KB
testcase_16 AC 59 ms
6,944 KB
testcase_17 AC 59 ms
6,940 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 58 ms
6,940 KB
testcase_22 AC 56 ms
6,940 KB
testcase_23 AC 57 ms
6,940 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

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