結果

問題 No.1141 田グリッド
ユーザー Moss_LocalMoss_Local
提出日時 2020-07-31 22:48:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 4,474 bytes
コンパイル時間 2,133 ms
コンパイル使用メモリ 209,000 KB
実行使用メモリ 6,440 KB
最終ジャッジ日時 2023-09-21 01:27:40
合計ジャッジ時間 5,979 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353 
#define MEM_SIZE 100010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }


//BELOW
struct mint
{
  int x; // typedef long long int;
  mint(int x = 0) : x((x % MOD + MOD) % MOD) {}
  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(int 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;
  }
};

const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit()
{
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++)
  {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int COM(int n, int k)
{
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int PERM(int n, int k)
{
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[n - k] % MOD) % MOD;
}

int MCOM(int n, int k)
{
  //n個のものから重複を許してr個選ぶ。
  //Choose R from N types of things, allowing for duplication.
  return COM(n + k - 1, k);
}
//solve

//-----CODE------//

void solve(void)
{
  int H,W; cin>>H; cin>>W;
  mint X = 1;
  vector<vector<int> >  mat (H,vector<int>(W,0));
  vector<int> zero_cnt_h(H,0);
  vector<int> zero_cnt_w(W,0);
  int zero_cnt = 0;
  for (int i = 0; i < H; i++)
  {
    for (int j = 0; j < W; j++)
    {
       cin>>mat[i][j];
       if(mat[i][j] == 0)
       {
         mat[i][j] = 0;
         zero_cnt++;
         zero_cnt_h[i]++;
         zero_cnt_w[j]++;
       }
       else
       {
        X *= mat[i][j];
       }
       

    }
  }
  vector<mint> hmul(H);
  vector<mint> wmul(W);
  for (int i = 0; i < H; i++)
  {
    hmul[i] = 1;
    for (int j = 0; j < W; j++)
    {
      if(mat[i][j] == 0)continue;
      hmul[i]*=mat[i][j];
    }
  }
  for (int j = 0; j < W; j++)
  {
    wmul[j] = 1;
    for (int i = 0; i < H; i++)
    {
      if(mat[i][j] == 0)continue;
      wmul[j] *= mat[i][j];
    }
    
  }
  
  
  
  
  int Q;cin>>Q;
  for (int q = 0; q < Q; q++)
  {
    int r,c;
    cin>>r>>c;
    r--;c--;
    int zero_erase = zero_cnt_w[c] + zero_cnt_h[r] - (mat[r][c] == 0);
    if(zero_cnt > zero_erase)
    {
      cout<<0<<endl;
      continue;
    }
    mint XX = X;
    XX /= hmul[r];
    XX /= wmul[c];
    if(mat[r][c] != 0)
    XX *= mat[r][c];
    cout<<XX.x<<endl;
  }
  
  return;
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}
0