結果

問題 No.1141 田グリッド
ユーザー HaarHaar
提出日時 2020-07-31 21:59:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 4,324 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 204,448 KB
実行使用メモリ 6,552 KB
最終ジャッジ日時 2023-09-20 23:14:38
合計ジャッジ時間 7,635 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 164 ms
4,376 KB
testcase_14 AC 181 ms
6,552 KB
testcase_15 AC 159 ms
4,380 KB
testcase_16 AC 159 ms
4,376 KB
testcase_17 AC 162 ms
4,380 KB
testcase_18 AC 160 ms
4,376 KB
testcase_19 AC 158 ms
4,380 KB
testcase_20 AC 158 ms
4,376 KB
testcase_21 AC 160 ms
4,380 KB
testcase_22 AC 162 ms
4,380 KB
testcase_23 AC 158 ms
4,376 KB
testcase_24 AC 159 ms
4,380 KB
testcase_25 AC 159 ms
4,384 KB
testcase_26 AC 158 ms
4,380 KB
testcase_27 AC 158 ms
4,388 KB
testcase_28 AC 158 ms
4,380 KB
testcase_29 AC 158 ms
4,380 KB
testcase_30 AC 159 ms
4,376 KB
testcase_31 AC 159 ms
4,380 KB
testcase_32 AC 162 ms
4,380 KB
testcase_33 AC 161 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

/**
 * @title Modint
 * @docs mint.md
 */
template <uint32_t M> class ModInt{
public:
  constexpr static uint32_t MOD = M;
  uint64_t val;
  
  constexpr ModInt(): val(0){}
  constexpr ModInt(int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  constexpr auto operator+(const ModInt &a) const {return ModInt(val + a.val);}
  constexpr auto operator-(const ModInt &a) const {return ModInt(val - a.val);}
  constexpr auto operator*(const ModInt &a) const {return ModInt(val * a.val);}
  constexpr auto operator/(const ModInt &a) const {return ModInt(val * a.inv().val);}
  
  constexpr auto& operator=(const ModInt &a){val = a.val; return *this;}
  constexpr auto& operator+=(const ModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  constexpr auto& operator-=(const ModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  constexpr auto& operator*=(const ModInt &a){(val *= a.val) %= M; return *this;}
  constexpr auto& operator/=(const ModInt &a){(val *= a.inv().val) %= M; return *this;}
  
  constexpr bool operator==(const ModInt &a) const {return val == a.val;}
  constexpr bool operator!=(const ModInt &a) const {return val != a.val;}
  
  constexpr auto& operator++(){*this += 1; return *this;}
  constexpr auto& operator--(){*this -= 1; return *this;}
  
  constexpr auto operator++(int){auto t = *this; *this += 1; return t;}
  constexpr auto operator--(int){auto t = *this; *this -= 1; return t;}
  
  constexpr static ModInt power(int64_t n, int64_t p){
    if(p < 0) return power(n, -p).inv();
    
    int64_t ret = 1, e = n % M;
    for(; p; (e *= e) %= M, p >>= 1) if(p & 1) (ret *= e) %= M;
    return ret;
  }
  
  constexpr static ModInt inv(int64_t a){
    int64_t b = M, u = 1, v = 0;
    
    while(b){
      int64_t t = a / b;
      a -= t * b; std::swap(a,b);
      u -= t * v; std::swap(u,v);
    }
    
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
  
  constexpr static auto frac(int64_t a, int64_t b){return ModInt(a) / ModInt(b);}
  
  constexpr auto power(int64_t p) const {return power(val, p);}
  constexpr auto inv() const {return inv(val);}
  
  friend constexpr auto operator-(const ModInt &a){return ModInt(-a.val);}
  
  friend constexpr auto operator+(int64_t a, const ModInt &b){return ModInt(a) + b;}
  friend constexpr auto operator-(int64_t a, const ModInt &b){return ModInt(a) - b;}
  friend constexpr auto operator*(int64_t a, const ModInt &b){return ModInt(a) * b;}
  friend constexpr auto operator/(int64_t a, const ModInt &b){return ModInt(a) / b;}
  
  friend std::istream& operator>>(std::istream &s, ModInt<M> &a){s >> a.val; return s;}
  friend std::ostream& operator<<(std::ostream &s, const ModInt<M> &a){s << a.val; return s;}

  template <int N>
  static auto div(){
    static auto value = inv(N);
    return value;
  }

  explicit operator int32_t() const noexcept {return val;}
  explicit operator int64_t() const noexcept {return val;}
};

using mint = ModInt<1000000007>;

/**
 * @docs input_vector.md
 */
template <typename T>
std::vector<T> input_vector(int N){
  std::vector<T> ret(N);
  for(int i = 0; i < N; ++i) std::cin >> ret[i];
  return ret;
}

template <typename T>
std::vector<std::vector<T>> input_vector(int N, int M){
  std::vector<std::vector<T>> ret(N);
  for(int i = 0; i < N; ++i) ret[i] = input_vector<T>(M);
  return ret;
}


int main(){
  int H, W; std::cin >> H >> W;
  auto A = input_vector<mint>(H, W);

  mint P = 1;
  int zero = 0;

  for(int i = 0; i < H; ++i){
    for(int j = 0; j < W; ++j){
      if(A[i][j] != 0) P *= A[i][j];
      else zero += 1;
    }
  }


  std::vector<mint> row(H, 1), col(W, 1);
  std::vector<int> row_zero(H, 0), col_zero(W, 0);
  for(int i = 0; i < H; ++i){
    for(int j = 0; j < W; ++j){
      if(A[i][j] != 0){
        row[i] *= A[i][j];
        col[j] *= A[i][j];
      }else{
        row_zero[i] += 1;
        col_zero[j] += 1;
      }
    }
  }

  int Q; std::cin >> Q;

  while(Q--){
    int r, c; std::cin >> r >> c;
    --r, --c;

    mint ans = P;
    ans /= row[r];
    ans /= col[c];
    if(A[r][c] != 0) ans *= A[r][c];

    int z = zero;
    z -= row_zero[r];
    z -= col_zero[c];
    if(A[r][c] == 0) z += 1;

    if(z) ans = 0;
    
    std::cout << ans << "\n";
  }

  return 0;
}
0