結果

問題 No.1141 田グリッド
ユーザー
提出日時 2021-03-19 20:03:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 1,499 bytes
コンパイル時間 928 ms
コンパイル使用メモリ 98,496 KB
実行使用メモリ 8,972 KB
最終ジャッジ日時 2024-11-18 18:18:42
合計ジャッジ時間 7,474 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
long long mod = 1000000007;
long long m_pow(long long x, long long y) {
	long long ans = 1;
	while (y > 0) {
		if ((y & 1) == 1) {
			ans = ans * x % mod;
		}
		x = x * x % mod;
		y >>= 1;
	}
	return ans;
}

vector<vector<long long>> a(100010);
long long zx[100010] = {}, zy[100010] = {};
long long mx[100010], my[100010];
int main() {
	int h, w;
	cin >> h >> w;
	long long m = 1, z = 0;
	for (int i = 0; i < h; i++) {
		mx[i] = 1;
	}
	for (int i = 0; i < w; i++) {
		my[i] = 1;
	}
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			long long a1;
			cin >> a1;
			a[i].emplace_back(a1);
			if (a1 != 0) {
				m *= a1, m %= mod;
				mx[i] *= a1, mx[i] %= mod;
				my[j] *= a1, my[j] %= mod;
			}
			else {
				zx[i]++, zy[j]++;
				z++;
			}
		}
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int r, c;
		cin >> r >> c;
		r--, c--;
		int z1 = zx[r] + zy[c];
		if (a[r][c] == 0)z1--;
		if (z - z1 == 0) {
			long long x = m_pow(mx[r], 1000000005);
			long long y = m_pow(my[c], 1000000005);
			long long ans = m;
			ans *= x, ans %= mod;
			ans *= y, ans %= mod;
			if (a[r][c] != 0) {
				ans *= a[r][c], ans %= mod;
			}
			cout << ans << endl;
		}
		else {
			cout << 0 << endl;
		}
	}
}
0