結果

問題 No.1141 田グリッド
ユーザー tonakaitonakai
提出日時 2020-07-31 22:00:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 7,133 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 207,124 KB
実行使用メモリ 53,040 KB
最終ジャッジ日時 2023-09-20 23:18:03
合計ジャッジ時間 23,205 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 177 ms
50,272 KB
testcase_01 AC 170 ms
50,248 KB
testcase_02 AC 164 ms
50,360 KB
testcase_03 AC 165 ms
50,320 KB
testcase_04 AC 178 ms
50,424 KB
testcase_05 AC 177 ms
50,288 KB
testcase_06 AC 176 ms
50,264 KB
testcase_07 AC 178 ms
50,260 KB
testcase_08 AC 171 ms
50,352 KB
testcase_09 AC 165 ms
50,332 KB
testcase_10 AC 174 ms
50,336 KB
testcase_11 AC 175 ms
50,412 KB
testcase_12 AC 176 ms
50,344 KB
testcase_13 AC 230 ms
51,064 KB
testcase_14 AC 245 ms
53,040 KB
testcase_15 AC 218 ms
50,800 KB
testcase_16 AC 219 ms
50,728 KB
testcase_17 AC 223 ms
50,828 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 219 ms
50,740 KB
testcase_22 AC 222 ms
50,912 KB
testcase_23 AC 219 ms
50,760 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define uniq(v) v.erase(unique(v.begin(),v.end()),v.end())
#define lb(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define ub(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
using Pair = pair<ll,pair<int,int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>> 
const ll mod=1000000007;
//const ll mod=998244353;
const ld pi=acos(-1.0);
const ll INF = 1LL<<61;
template<class T>bool chmax(T &a, const T &b) { 
  if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) {
  if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下20桁まで
//実行時間制約2秒では2×10^8回くらいまで計算できる

//——————————————————modint———————————————————————————


#define MAX 2000000  // 階乗をいくつまで計算するか
 
class mint {
    ll val;
    static ll *invs, *facts, *finvs;
 
    // 階乗, 逆元, 階乗の逆元をMAXまで求める
    bool initMint() {
        invs[1] = 
        facts[0] = facts[1] =
        finvs[0] = finvs[1] = 1;
        for (int i=2; i<=MAX; i++) {
            invs[i]  = -invs[MOD % i] * (MOD / i) % MOD;
            facts[i] = facts[i - 1] * i % MOD;
            finvs[i] = finvs[i - 1] * invs[i] % MOD;
        }
        return true;
    }
 
public:
    static ll MOD; // modの元
 
    // 初期化 値を引数に与えなかった場合はval=0としておく
    mint(ll init = 0) : val(init) {
        static bool call_once = initMint(); // static変数の性質により一度だけ呼ばれる
        assert(call_once); // unusedの回避
        if (val < 0 || val >= MOD) val %= MOD;
        if (val < 0) val += MOD;   // 0以上であることを保証
    }
 
    inline ll toll() { return this->val; }
 
    // 代入
    void operator=(const mint &r) { this->val = r.val; }
    void operator=(const ll &r) { *this = mint(r); }
 
    // 足し算; 符号反転; 引き算
    mint operator+(const mint &r) {
        ll ans = this->val + r.val;
        if (ans >= MOD) ans -= MOD;
        return mint(ans);
    }
    mint operator-() {
        ll ans = MOD - this->val;
        return mint(ans);
    }
    mint operator-(const mint &r) {
        mint rr = r;
        return *this + (-rr);
    }
 
    //かけ算; 逆元; わり算
    mint operator*(const mint &r) {
        ll ans = this->val * r.val;
        return mint(ans);
    }
    mint inv() {
        assert(this->val != 0);
        if (this->val == 1) return mint(1);
 
        mint p, q = *this, m(0), n(1), r, c;
        p.val = MOD;
        while (q.val > MAX) {
            r = p.val % q.val;
            c = m.val - n.val * (p.val / q.val);
            p = q, q = r, m = n, n = c;
        }
        return n * invs[q.val];
    }
    mint operator/(const mint &r) { return *this * mint(r).inv(); }
    mint operator%(const mint &r) { return mint(this->val % r.val); }
 
    // ++ -- 前付きと後ろ付き
    void operator++() { ++this->val; }
    void operator++(int a) {
        a = 0;
        this->val++;
    }
    void operator--() { --this->val; }
    void operator--(int a) {
        a = 0;
        this->val--;
    }
 
    // 四則演算&代入
    void operator+=(const mint &r) { *this = *this + r; }
    void operator-=(const mint &r) { *this = *this - r; }
    void operator*=(const mint &r) { *this = *this * r; }
    void operator/=(const mint &r) { *this = *this / r; }
    void operator%=(const mint &r) { *this = *this % r; }
 
    // べき乗
    mint pow(long n) {
        if (n < 0)
            return inv().pow(-n);  // 逆元の-n乗
        else if (n == 0)
            return mint(1);
 
        mint half = pow(n / 2);
        if (n % 2)
            return *this * half * half;
        else
            return half * half;
    }
    mint pow(mint n) { return pow(n.val); }
 
    // 順列
    mint per(mint _k) {
        assert(this->val <= MAX);
        const ll n = this->val, k = _k.val;
        if (k < 0 || k > n) return 0;
        if (k == 0) return 1;
        if (k == n) return facts[n];
        return mint(facts[n]) * finvs[n - k];
    }

    // コンビネーション
    mint com(mint _k) {
        assert(this->val <= MAX);
        const ll n = this->val, k = _k.val;
        if (k < 0 || k > n) return 0;
        if (k == 0 || k == n) return 1;
        return mint(facts[n]) * finvs[k] * finvs[n - k];
    }
 
    // 階乗
    mint fact() { 
        assert(this->val <= MAX);
        return mint(facts[this->val]);
    }
 
    friend bool operator<(const mint &l, const mint &r) { return l.val < r.val; }
    friend bool operator>(const mint &l, const mint &r) { return l.val > r.val; }
    friend bool operator==(const mint &l, const mint &r) { return l.val == r.val; }
    friend bool operator!=(const mint &l, const mint &r) { return !(l.val == r.val); }
    friend bool operator<=(const mint &l, const mint &r) { return !(l.val > r.val); }
    friend bool operator>=(const mint &l, const mint &r) { return !(l.val < r.val); }
 
    friend ostream &operator<<(ostream &os, const mint &out) {
        os << out.val;
        return os;
    }
    friend istream &operator>>(istream &is, mint &in) {
        ll inl;
        is >> inl;
        in.val = inl % mint::MOD;
        return is;
    }
};
 
// べき乗
inline mint mpow(ll n, ll k) { return mint(n).pow(k); }
// 順列
inline mint mper(ll n, ll k) { return mint(n).per(k); }
// コンビネーション
inline mint mcom(ll n, ll k) { return mint(n).com(k); }
// 階乗
inline mint mfac(ll n) { return mint(n).fact(); }
 
// static変数
ll *mint::invs  = new ll[MAX+1];
ll *mint::facts = new ll[MAX+1];
ll *mint::finvs = new ll[MAX+1];
 
ll mint::MOD = (ll)1e9 + 7;

//—————————————————————————————————————————————————



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

  int h,w;
  cin>>h>>w;

  vector<vector<mint>> p(h,vector<mint>(w));
  rep(i,h){
    rep(j,w){
      cin>>p[i][j];
    }
  }

  vector<mint> gy(h);
  vector<mint> re(w);
  rep(i,h){
    gy[i]=p[i][0];
  }
  rep(i,h){
    repo(j,w){
      gy[i]*=p[i][j];
    }
  }

  re=p[0];
  rep(j,w){
    repo(i,h){
      re[j]*=p[i][j];
    }
  }

  mint zen=1;
  rep(i,h){
    zen*=gy[i];
  }

  int q;
  cin>>q;
  rep(i,q){
    int r,c;
    cin>>r>>c;
    r--;c--;
    mint ans=zen*p[r][c];
    ans*=gy[r].inv()*re[c].inv();
    cout << ans << endl;
  }


}
0