結果

問題 No.1141 田グリッド
ユーザー iqueue02iqueue02
提出日時 2020-07-31 23:03:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,844 bytes
コンパイル時間 2,107 ms
コンパイル使用メモリ 210,940 KB
実行使用メモリ 16,736 KB
最終ジャッジ日時 2023-09-21 02:05:19
合計ジャッジ時間 8,445 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 11 ms
4,376 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 175 ms
6,560 KB
testcase_14 AC 203 ms
16,736 KB
testcase_15 AC 214 ms
7,000 KB
testcase_16 AC 213 ms
7,128 KB
testcase_17 AC 213 ms
7,068 KB
testcase_18 AC 211 ms
7,048 KB
testcase_19 AC 210 ms
7,088 KB
testcase_20 AC 212 ms
6,960 KB
testcase_21 AC 213 ms
6,940 KB
testcase_22 AC 215 ms
6,940 KB
testcase_23 AC 215 ms
6,976 KB
testcase_24 AC 213 ms
6,940 KB
testcase_25 AC 211 ms
6,488 KB
testcase_26 AC 213 ms
7,016 KB
testcase_27 AC 212 ms
7,076 KB
testcase_28 AC 213 ms
6,452 KB
testcase_29 AC 217 ms
6,972 KB
testcase_30 AC 213 ms
7,040 KB
testcase_31 AC 214 ms
7,016 KB
testcase_32 AC 215 ms
7,036 KB
testcase_33 AC 213 ms
7,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 mod
  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;}
};

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;
} 
0