結果

問題 No.1141 田グリッド
ユーザー kk
提出日時 2020-09-05 05:15:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 1,486 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 204,748 KB
実行使用メモリ 6,412 KB
最終ジャッジ日時 2023-08-18 05:25:55
合計ジャッジ時間 6,303 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 104 ms
4,380 KB
testcase_14 AC 117 ms
6,412 KB
testcase_15 AC 102 ms
4,380 KB
testcase_16 AC 100 ms
4,376 KB
testcase_17 AC 103 ms
4,376 KB
testcase_18 AC 86 ms
4,380 KB
testcase_19 AC 88 ms
4,380 KB
testcase_20 AC 88 ms
4,380 KB
testcase_21 AC 103 ms
4,376 KB
testcase_22 AC 104 ms
4,380 KB
testcase_23 AC 102 ms
4,380 KB
testcase_24 AC 89 ms
4,376 KB
testcase_25 AC 89 ms
4,376 KB
testcase_26 AC 90 ms
4,376 KB
testcase_27 AC 88 ms
4,376 KB
testcase_28 AC 89 ms
4,380 KB
testcase_29 AC 87 ms
4,380 KB
testcase_30 AC 86 ms
4,380 KB
testcase_31 AC 88 ms
4,380 KB
testcase_32 AC 87 ms
4,380 KB
testcase_33 AC 88 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

const long long MOD = 1e9 + 7;

long long modpow(long long x, long long p, long long mod) {
  long long ret = 1;
  while (p) {
    if (p & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w;
  cin >> h >> w;
  vector<vector<int> > a(h, vector<int>(w));

  long long sum = 1;
  int zero = 0;
  REP (i, h) REP (j, w) {
    cin >> a[i][j];
    if (a[i][j])
      sum = sum * a[i][j] % MOD;
    else
      ++zero;
  }
  
  vector<long long> hsum(h), wsum(w);
  vector<int> hzero(h), wzero(w);
  REP (i, h) {
    hsum[i] = 1;
    hzero[i] = 0;
    REP (j, w) {
      if (a[i][j])
        hsum[i] = hsum[i] * a[i][j] % MOD;
      else
        hzero[i]++;
    }
  }
  REP (j, w) {
    wsum[j] = 1;
    wzero[j] = 0;
    REP (i, h) {
      if (a[i][j])
        wsum[j] = wsum[j] * a[i][j] % MOD;
      else
        wzero[j]++;
    }
  }
  
  int q;
  cin >> q;
  while (q--) {
    int r, c;
    cin >> r >> c;
    --r, --c;

    int z = hzero[r] + wzero[c] - !a[r][c];
    if (z < zero)
      cout << 0 << endl;
    else {
      long long ret = 1;
      ret = ret * sum % MOD;
      ret = ret * modpow(hsum[r], MOD-2, MOD) % MOD;
      ret = ret * modpow(wsum[c], MOD-2, MOD) % MOD;
      if (a[r][c])
        ret = ret * a[r][c] % MOD;
      cout << ret << endl;
    }
  }
    
  return 0;
}
0