結果

問題 No.1141 田グリッド
ユーザー nikkukunnikkukun
提出日時 2020-07-31 22:00:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,627 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 165,820 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 23:16:30
合計ジャッジ時間 5,258 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

ll QPow(ll bas, int t) {
	ll ret = 1;
	while (t) {
		if (t & 1) ret = ret * bas % MOD;
		bas = bas * bas % MOD;
		t >>= 1;
	}
	return ret;
}

ll Inv(ll x) {
	return QPow(x, MOD - 2);
}

int n, m;

int Idx(int x, int y) {
	return (x - 1) * m + y - 1;
}

int a[N];
ll r[N], c[N];
int r0[N], c0[N];

int main() {
	ios::sync_with_stdio(0);

	cin >> n >> m;
	int sum0 = 0;
	ll prod = 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			cin >> a[Idx(i, j)];
			if (a[Idx(i, j)] == 0)
				sum0++;
			else
				prod = prod * a[Idx(i, j)] % MOD;
		}

	for (int i = 1; i <= n; i++) {
		r[i] = 1;
		for (int j = 1; j <= m; j++) {
			if (a[Idx(i, j)] == 0)
				r0[i]++;
			else
				r[i] = r[i] * a[Idx(i, j)] % MOD;
		}
	}

	for (int j = 1; j <= m; j++) {
		c[j] = 1;
		for (int i = 1; i <= n; i++) {
			if (a[Idx(i, j)] == 0)
				c0[j]++;
			else
				c[j] = c[j] * a[Idx(i, j)] % MOD;
		}
	}

	int q;
	cin >> q;
	while (q--) {
		int x, y;
		cin >> x >> y;
		int res = sum0 - r0[x] - c0[y] + (a[Idx(x, y)] == 0);
		if (res) {
			cout << 0 << endl;
		} else {
			ll ans = prod * Inv(r[x]) % MOD * Inv(c[y]) % MOD;
			if (a[Idx(x, y)]) ans = ans * a[Idx(x, y)] % MOD;
			cout << ans << endl;
		}
	}

	return 0;
}
0