結果

問題 No.1141 田グリッド
ユーザー どららどらら
提出日時 2020-08-01 01:26:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 2,275 bytes
コンパイル時間 1,904 ms
コンパイル使用メモリ 176,464 KB
実行使用メモリ 16,904 KB
最終ジャッジ日時 2023-09-21 05:31:14
合計ジャッジ時間 6,786 ms
ジャッジサーバーID
(参考情報)
judge12 / 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,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 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,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 141 ms
6,904 KB
testcase_14 AC 167 ms
16,904 KB
testcase_15 AC 137 ms
7,008 KB
testcase_16 AC 137 ms
6,944 KB
testcase_17 AC 138 ms
7,048 KB
testcase_18 AC 136 ms
6,980 KB
testcase_19 AC 134 ms
7,060 KB
testcase_20 AC 135 ms
7,012 KB
testcase_21 AC 136 ms
6,948 KB
testcase_22 AC 136 ms
7,060 KB
testcase_23 AC 138 ms
6,908 KB
testcase_24 AC 134 ms
6,912 KB
testcase_25 AC 134 ms
6,464 KB
testcase_26 AC 137 ms
7,064 KB
testcase_27 AC 136 ms
7,124 KB
testcase_28 AC 135 ms
6,424 KB
testcase_29 AC 133 ms
6,928 KB
testcase_30 AC 136 ms
6,996 KB
testcase_31 AC 136 ms
6,480 KB
testcase_32 AC 134 ms
6,904 KB
testcase_33 AC 133 ms
6,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const ll MOD = 1000000007;

int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<int>> v(H, vector<int>(W));
  rep(i,H){
    rep(j,W){
      cin >> v[i][j];
    }
  }

  vector<vector<ll>> dp1(H, vector<ll>(W));
  vector<vector<ll>> dp2(H, vector<ll>(W));
  vector<vector<ll>> dp3(H, vector<ll>(W));
  vector<vector<ll>> dp4(H, vector<ll>(W));

  {
    ll p = 1;
    for(int y=0; y<H; y++){
      p *= v[y][0];
      p %= MOD;
      dp1[y][0] = p;
    }
    for(int x=1; x<W; x++){
      p = 1;
      for(int y=0; y<H; y++){
        p *= v[y][x];
        p %= MOD;
        dp1[y][x] = (dp1[y][x-1] * p)%MOD;
      }
    }
  }
  {
    ll p = 1;
    for(int y=0; y<H; y++){
      p *= v[y][W-1];
      p %= MOD;
      dp2[y][W-1] = p;
    }
    for(int x=W-2; x>=0; x--){
      p = 1;
      for(int y=0; y<H; y++){
        p *= v[y][x];
        p %= MOD;
        dp2[y][x] = (dp2[y][x+1] * p)%MOD;
      }
    }
  }
  {
    ll p = 1;
    for(int y=H-1; y>=0; y--){
      p *= v[y][0];
      p %= MOD;
      dp3[y][0] = p;
    }
    for(int x=1; x<W; x++){
      p = 1;
      for(int y=H-1; y>=0; y--){
        p *= v[y][x];
        p %= MOD;
        dp3[y][x] = (dp3[y][x-1] * p)%MOD;
      }
    }
  }
  {
    ll p = 1;
    for(int y=H-1; y>=0; y--){
      p *= v[y][W-1];
      p %= MOD;
      dp4[y][W-1] = p;
    }
    for(int x=W-2; x>=0; x--){
      p = 1;
      for(int y=H-1; y>=0; y--){
        p *= v[y][x];
        p %= MOD;
        dp4[y][x] = (dp4[y][x+1] * p)%MOD;
      }
    }
  }
  
  int Q;
  cin >> Q;
  rep(i,Q){
    int r, c;
    cin >> r >> c;
    r--; c--;

    ll ret = 1;
    if(r != 0 && c != 0){
      ret *= dp1[r-1][c-1];
      ret %= MOD;
    }
    if(r != 0 && c != W-1){
      ret *= dp2[r-1][c+1];
      ret %= MOD;
    }
    if(r != H-1 && c != 0){
      ret *= dp3[r+1][c-1];
      ret %= MOD;
    }
    if(r != H-1 && c != W-1){
      ret *= dp4[r+1][c+1];
      ret %= MOD;
    }
    cout << ret << endl;
  }
  

  return 0;
}

0