結果

問題 No.1141 田グリッド
ユーザー ks2mks2m
提出日時 2020-07-31 21:56:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 496 ms / 2,000 ms
コード長 2,122 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 74,180 KB
実行使用メモリ 64,968 KB
最終ジャッジ日時 2023-09-20 23:08:19
合計ジャッジ時間 14,395 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,468 KB
testcase_01 AC 44 ms
47,284 KB
testcase_02 AC 43 ms
49,424 KB
testcase_03 AC 43 ms
49,548 KB
testcase_04 AC 43 ms
49,332 KB
testcase_05 AC 44 ms
49,308 KB
testcase_06 AC 47 ms
49,816 KB
testcase_07 AC 45 ms
49,416 KB
testcase_08 AC 96 ms
52,680 KB
testcase_09 AC 88 ms
52,828 KB
testcase_10 AC 90 ms
52,740 KB
testcase_11 AC 104 ms
52,492 KB
testcase_12 AC 90 ms
53,108 KB
testcase_13 AC 496 ms
64,968 KB
testcase_14 AC 480 ms
59,780 KB
testcase_15 AC 433 ms
58,116 KB
testcase_16 AC 423 ms
58,788 KB
testcase_17 AC 418 ms
57,996 KB
testcase_18 AC 330 ms
58,276 KB
testcase_19 AC 329 ms
58,000 KB
testcase_20 AC 322 ms
58,488 KB
testcase_21 AC 451 ms
57,972 KB
testcase_22 AC 449 ms
58,388 KB
testcase_23 AC 438 ms
58,128 KB
testcase_24 AC 324 ms
58,696 KB
testcase_25 AC 340 ms
58,332 KB
testcase_26 AC 334 ms
58,280 KB
testcase_27 AC 337 ms
58,224 KB
testcase_28 AC 346 ms
58,524 KB
testcase_29 AC 350 ms
57,952 KB
testcase_30 AC 323 ms
58,240 KB
testcase_31 AC 330 ms
57,832 KB
testcase_32 AC 333 ms
58,652 KB
testcase_33 AC 335 ms
58,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int h = Integer.parseInt(sa[0]);
		int w = Integer.parseInt(sa[1]);
		int[][] a = new int[h][w];
		for (int i = 0; i < h; i++) {
			sa = br.readLine().split(" ");
			for (int j = 0; j < w; j++) {
				a[i][j] = Integer.parseInt(sa[j]);
			}
		}
		int q = Integer.parseInt(br.readLine());
		int[] r = new int[q];
		int[] c = new int[q];
		for (int i = 0; i < q; i++) {
			sa = br.readLine().split(" ");
			r[i] = Integer.parseInt(sa[0]) - 1;
			c[i] = Integer.parseInt(sa[1]) - 1;
		}
		br.close();

		int mod = 1000000007;
		int zero = 0;
		long total = 1;

		long[] sx = new long[h];
		int[] zx = new int[h];
		for (int i = 0; i < h; i++) {
			sx[i] = 1;
			for (int j = 0; j < w; j++) {
				if (a[i][j] == 0) {
					zero++;
					zx[i]++;
				} else {
					total *= a[i][j];
					total %= mod;
					sx[i] *= a[i][j];
					sx[i] %= mod;
				}
			}
		}

		long[] sy = new long[w];
		int[] zy = new int[w];
		for (int j = 0; j < w; j++) {
			sy[j] = 1;
			for (int i = 0; i < h; i++) {
				if (a[i][j] == 0) {
					zy[j]++;
				} else {
					sy[j] *= a[i][j];
					sy[j] %= mod;
				}
			}
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < q; i++) {
			int z = zero - zx[r[i]] - zy[c[i]];
			if (a[r[i]][c[i]] == 0) {
				z++;
			}
			if (z == 0) {
				long ans = total * modinv(sx[r[i]], mod) % mod;
				ans *= modinv(sy[c[i]], mod) % mod;
				ans %= mod;
				if (a[r[i]][c[i]] != 0) {
					ans *= a[r[i]][c[i]] % mod;
					ans %= mod;
				}
				pw.println(ans);
			} else {
				pw.println(0);
			}
		}
		pw.flush();
	}

	static long modinv(long a, int m) {
		long b = m;
		long u = 1;
		long v = 0;
		long tmp = 0;

		while (b > 0) {
			long t = a / b;
			a -= t * b;
			tmp = a;
			a = b;
			b = tmp;

			u -= t * v;
			tmp = u;
			u = v;
			v = tmp;
		}

		u %= m;
		if (u < 0) u += m;
		return u;
	}
}
0