結果

問題 No.1141 田グリッド
ユーザー chocono2230chocono2230
提出日時 2020-07-31 22:38:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 344 ms / 2,000 ms
コード長 2,551 bytes
コンパイル時間 1,723 ms
コンパイル使用メモリ 176,824 KB
実行使用メモリ 6,208 KB
最終ジャッジ日時 2023-09-21 01:10:29
合計ジャッジ時間 6,896 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 153 ms
4,380 KB
testcase_14 AC 161 ms
6,208 KB
testcase_15 AC 148 ms
4,380 KB
testcase_16 AC 149 ms
4,380 KB
testcase_17 AC 150 ms
4,376 KB
testcase_18 AC 134 ms
4,376 KB
testcase_19 AC 138 ms
4,376 KB
testcase_20 AC 140 ms
4,376 KB
testcase_21 AC 149 ms
4,380 KB
testcase_22 AC 149 ms
4,400 KB
testcase_23 AC 149 ms
4,376 KB
testcase_24 AC 123 ms
4,380 KB
testcase_25 AC 126 ms
4,376 KB
testcase_26 AC 123 ms
4,376 KB
testcase_27 AC 123 ms
4,488 KB
testcase_28 AC 126 ms
4,376 KB
testcase_29 AC 188 ms
4,376 KB
testcase_30 AC 154 ms
4,376 KB
testcase_31 AC 344 ms
4,380 KB
testcase_32 AC 125 ms
4,376 KB
testcase_33 AC 124 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
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 {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=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 {
    mint res(*this);
    return res/=a;
  }
};

int main(){
  int h, w;
  cin >> h >> w;
  vector<vector<mint>> a(h, vector<mint>(w));
  set<pair<int, int>> zero;
  rep(i, h){
    rep(j, w){
      int in;
      cin >> in;
      if(in == 0){
        zero.insert({i, j});
        in = 1;
      }
      a.at(i).at(j) = in;
    }
  }

  vector<mint> hp(h, 1), wp(w, 1);
  mint tot = 1;
  rep(i, h){
    rep(j, w){
      wp.at(j) *= a.at(i).at(j);
      hp.at(i) *= a.at(i).at(j);
      tot *= a.at(i).at(j);
    }
  }

  int qe;
  cin >> qe;
rep(_q, qe){
  int r, c;
  cin >> r >> c;
  r--; c--;
  mint ans = tot;
  if(zero.size() != 0){
    bool f = false;
    for(auto p : zero){
      if(p.first != r && p.second != c) f = true;
    }
    if(f == true){
      ans = 0;
      cout << ans.x << endl;
      continue;
    }
  }
  ans *= a.at(r).at(c);
  ans /= hp.at(r);
  ans /= wp.at(c);
  cout << ans.x << endl;
}

  return 0;
}
0