結果

問題 No.1141 田グリッド
ユーザー mmn15277198mmn15277198
提出日時 2022-09-11 00:43:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 172,320 KB
実行使用メモリ 16,764 KB
最終ジャッジ日時 2023-08-18 05:23:43
合計ジャッジ時間 7,065 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 145 ms
6,856 KB
testcase_14 AC 172 ms
16,764 KB
testcase_15 AC 141 ms
7,392 KB
testcase_16 AC 142 ms
7,488 KB
testcase_17 AC 145 ms
7,460 KB
testcase_18 AC 144 ms
7,484 KB
testcase_19 AC 140 ms
7,368 KB
testcase_20 AC 144 ms
7,392 KB
testcase_21 AC 143 ms
7,332 KB
testcase_22 AC 142 ms
7,324 KB
testcase_23 AC 142 ms
7,584 KB
testcase_24 AC 140 ms
7,368 KB
testcase_25 AC 142 ms
7,032 KB
testcase_26 AC 141 ms
7,384 KB
testcase_27 AC 139 ms
7,428 KB
testcase_28 AC 144 ms
7,032 KB
testcase_29 AC 139 ms
7,308 KB
testcase_30 AC 142 ms
7,364 KB
testcase_31 AC 139 ms
7,344 KB
testcase_32 AC 140 ms
7,392 KB
testcase_33 AC 141 ms
7,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<long long>> a(h, vector<long long>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  vector<vector<long long>> b = a;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j + 1 < w) {
        b[i][j + 1] *= b[i][j];
        b[i][j + 1] %= mod;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i + 1 < h) {
        b[i + 1][j] *= b[i][j];
        b[i + 1][j] %= mod;
      }
    }
  }

  vector<vector<long long>> c = a;
  for (int i = 0; i < h; i++) {
    for (int j = w - 1; j >= 0; j--) {
      if (j - 1 >= 0) {
        c[i][j - 1] *= c[i][j];
        c[i][j - 1] %= mod;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i + 1 < h) {
        c[i + 1][j] *= c[i][j];
        c[i + 1][j] %= mod;
      }
    }
  }

  vector<vector<long long>> d = a;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j + 1 < w) {
        d[i][j + 1] *= d[i][j];
        d[i][j + 1] %= mod;
      }
    }
  }
  for (int i = h - 1; i >= 0; i--) {
    for (int j = 0; j < w; j++) {
      if (i - 1 >= 0) {
        d[i - 1][j] *= d[i][j];
        d[i - 1][j] %= mod;
      }
    }
  }

  vector<vector<long long>> e = a;
  for (int i = 0; i < h; i++) {
    for (int j = w - 1; j >= 0; j--) {
      if (j - 1 >= 0) {
        e[i][j - 1] *= e[i][j];
        e[i][j - 1] %= mod;
      }
    }
  }
  for (int i = h - 1; i >= 0; i--) {
    for (int j = 0; j < w; j++) {
      if (i - 1 >= 0) {
        e[i - 1][j] *= e[i][j];
        e[i - 1][j] %= mod;
      }
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    long long ans = 1;
    if (x - 1 >= 0 && y - 1 >= 0) ans *= b[x - 1][y - 1];
    ans %= mod;
    if (x - 1 >= 0 && y + 1 < w) ans *= c[x - 1][y + 1];
    ans %= mod;
    if (x + 1 < h && y - 1 >= 0) ans *= d[x + 1][y - 1];
    ans %= mod;
    if (x + 1 < h && y + 1 < w) ans *= e[x + 1][y + 1];
    ans %= mod;
    cout << ans << endl;
  }
  return 0;
}
0