結果
問題 | No.1141 田グリッド |
ユーザー |
![]() |
提出日時 | 2020-07-31 23:03:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 261 ms / 2,000 ms |
コード長 | 2,844 bytes |
コンパイル時間 | 2,599 ms |
コンパイル使用メモリ | 207,028 KB |
最終ジャッジ日時 | 2025-01-12 11:25:45 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define rep(i,n) for(int i = 0; i < (n);i++)#define sz(x) int(x.size())typedef unsigned long long ll;typedef long double ld;typedef pair<int,int> P;constexpr int mod = 1e9+7;struct mint {ll x; // typedef long long ll;mint(ll 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(ll t) const {if (!t) return 1;mint a = pow(t>>1);a *= a;if (t&1) a *= *this;return a;}// for prime modmint 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;}};typedef vector<mint> vc;typedef vector<vc> vvc;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];vvc dp1(h, vc(w, 0));auto dp2 = dp1;auto dp3 = dp1;auto dp4 = dp1;for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {dp1[i][j] = a[i][j];if (i > 0) dp1[i][j] *= dp1[i - 1][j];if (j > 0) dp1[i][j] *= dp1[i][j - 1];if (i > 0 && j > 0) dp1[i][j] /= dp1[i - 1][j - 1];}}for (int i = h - 1; i >= 0; i--) {for (int j = w - 1; j >= 0; j--) {dp2[i][j] = a[i][j];if (i < h - 1) dp2[i][j] *= dp2[i + 1][j];if (j < w - 1) dp2[i][j] *= dp2[i][j + 1];if (i < h - 1 && j < w - 1) dp2[i][j] /= dp2[i + 1][j + 1];}}for (int i = 0; i < h; i++) {for (int j = w - 1; j >= 0; j--) {dp3[i][j] = a[i][j];if (i > 0) dp3[i][j] *= dp3[i - 1][j];if (j < w - 1) dp3[i][j] *= dp3[i][j + 1];if (i > 0 && j < w - 1) dp3[i][j] /= dp3[i - 1][j + 1];}}for (int i = h - 1; i >= 0; i--) {for (int j = 0; j < w; j++) {dp4[i][j] = a[i][j];if (i < h - 1) dp4[i][j] *= dp4[i + 1][j];if (j > 0) dp4[i][j] *= dp4[i][j - 1];if (i < h - 1 && j > 0) dp4[i][j] /= dp4[i + 1][j - 1];}}int q;cin >> q;while (q--) {int r, c;cin >> r >> c;r--; c--;mint res = 1;if (r - 1 >= 0 && c - 1 >= 0) res *= dp1[r - 1][c - 1];if (r - 1 >= 0 && c + 1 <= w - 1) res *= dp3[r - 1][c + 1];if (r + 1 <= h - 1 && c + 1 <= w - 1) res *= dp2[r + 1][c + 1];if (r + 1 <= h - 1 && c - 1 >= 0) res *= dp4[r + 1][c - 1];cout << res.x << "\n";}return 0;}