結果

問題 No.1141 田グリッド
ユーザー Example0911Example0911
提出日時 2020-07-31 23:13:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 2,242 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 174,292 KB
実行使用メモリ 6,520 KB
最終ジャッジ日時 2023-09-21 02:27:35
合計ジャッジ時間 5,485 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 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,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 113 ms
4,548 KB
testcase_14 AC 120 ms
6,520 KB
testcase_15 AC 113 ms
4,376 KB
testcase_16 AC 113 ms
4,376 KB
testcase_17 AC 113 ms
4,376 KB
testcase_18 AC 90 ms
4,376 KB
testcase_19 AC 90 ms
4,376 KB
testcase_20 AC 88 ms
4,380 KB
testcase_21 AC 113 ms
4,380 KB
testcase_22 AC 113 ms
4,380 KB
testcase_23 AC 114 ms
4,380 KB
testcase_24 AC 87 ms
4,380 KB
testcase_25 AC 90 ms
4,380 KB
testcase_26 AC 88 ms
4,376 KB
testcase_27 AC 89 ms
4,380 KB
testcase_28 AC 90 ms
4,380 KB
testcase_29 AC 87 ms
4,376 KB
testcase_30 AC 88 ms
4,504 KB
testcase_31 AC 88 ms
4,376 KB
testcase_32 AC 87 ms
4,376 KB
testcase_33 AC 89 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define rep(i,n) for(ll i = 0; i < (n); i++)
const int inf = 1e9 + 11;
int MOD = 1e9 + 7;
const ll INF = 1LL << 60;
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() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w; cin >> h >> w;
	mint ans = 1;
	vector<vector<ll>>a(h, vector<ll>(w));
	bool ok = false;
	int cnt = 0;
	rep(i, h)rep(j, w)cin >> a[i][j];
	rep(i, h)rep(j, w) {
		if (a[i][j] == 0) {
			ok = true; cnt++; continue;
		}
		ans *= a[i][j];
	}
	vector<mint>r(h+1000, 1), c(w+1999, 1);
	vector<int>isr(h), isc(w);
	rep(i, h) {
		rep(j, w) {
			if (a[i][j] == 0) {
				isr[i]++;  continue;
			}
			r[i] *= a[i][j];
		}
	}
	rep(j, w) {
		rep(i, h) {
			if (a[i][j] == 0) {
				isc[j]++; continue;
			}
			c[j] *= a[i][j];
		}
	}
	int q; cin >> q;
	rep(_, q) {
		ll x, y; cin >> x >> y; x--; y--;
		if (!ok) {
			mint now = ans;
			mint tmp = r[x]; tmp *= c[y];
			tmp /= a[x][y];
			now /= tmp;
			cout << now.x << endl;
		}
		else {
			
				if (isr[x] + isc[y] - (int)(a[x][y] == 0)== cnt) {
					mint now = ans;
					mint tmp = 1; 
					tmp *= r[x];
					tmp *= c[y];
					if(a[x][y] != 0)tmp /= a[x][y];
					now /= tmp;
					cout << now.x << endl;
				}
				else cout << 0 << endl;
			
		}
	}
	return 0;
}
0