結果

問題 No.1141 田グリッド
ユーザー mmn15277198mmn15277198
提出日時 2022-09-11 00:43:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 1,514 ms
コンパイル使用メモリ 174,560 KB
実行使用メモリ 16,896 KB
最終ジャッジ日時 2024-05-05 11:37:56
合計ジャッジ時間 5,845 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 125 ms
7,128 KB
testcase_14 AC 142 ms
16,896 KB
testcase_15 AC 130 ms
7,552 KB
testcase_16 AC 122 ms
7,424 KB
testcase_17 AC 125 ms
7,680 KB
testcase_18 AC 122 ms
7,552 KB
testcase_19 AC 128 ms
7,424 KB
testcase_20 AC 122 ms
7,552 KB
testcase_21 AC 121 ms
7,424 KB
testcase_22 AC 123 ms
7,552 KB
testcase_23 AC 120 ms
7,424 KB
testcase_24 AC 122 ms
7,424 KB
testcase_25 AC 122 ms
7,296 KB
testcase_26 AC 118 ms
7,552 KB
testcase_27 AC 127 ms
7,552 KB
testcase_28 AC 127 ms
7,168 KB
testcase_29 AC 121 ms
7,552 KB
testcase_30 AC 120 ms
7,552 KB
testcase_31 AC 119 ms
7,424 KB
testcase_32 AC 132 ms
7,424 KB
testcase_33 AC 121 ms
7,424 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