結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー lavoxlavox
提出日時 2021-07-09 22:11:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,065 ms / 2,000 ms
コード長 1,765 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 74,636 KB
実行使用メモリ 94,748 KB
最終ジャッジ日時 2023-09-14 09:22:13
合計ジャッジ時間 16,809 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
84,348 KB
testcase_01 AC 130 ms
84,176 KB
testcase_02 AC 1,054 ms
94,708 KB
testcase_03 AC 1,024 ms
94,384 KB
testcase_04 AC 1,065 ms
93,928 KB
testcase_05 AC 1,043 ms
93,612 KB
testcase_06 AC 1,025 ms
94,028 KB
testcase_07 AC 1,046 ms
94,076 KB
testcase_08 AC 1,063 ms
94,032 KB
testcase_09 AC 1,034 ms
93,672 KB
testcase_10 AC 1,008 ms
94,748 KB
testcase_11 AC 957 ms
90,888 KB
testcase_12 AC 952 ms
91,520 KB
testcase_13 AC 937 ms
91,644 KB
testcase_14 AC 107 ms
56,288 KB
testcase_15 AC 106 ms
55,856 KB
testcase_16 AC 120 ms
56,108 KB
testcase_17 AC 105 ms
55,720 KB
testcase_18 AC 106 ms
55,448 KB
testcase_19 AC 110 ms
55,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	static long[] frac = null;
	static final int MOD = 1000000007;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		int M = Integer.parseInt(sc.next());
		int[] t = new int[M];
		int[] x = new int[M];
		int[] y = new int[M];
		for ( int i = 0 ; i < M ; i++ ) {
			t[i] = Integer.parseInt(sc.next());
			x[i] = Integer.parseInt(sc.next());
			y[i] = Integer.parseInt(sc.next());
		}
		sc.close();
		
		frac = new long[2 * N + 2];
		rec(2 * N + 1);
		
		long ans = (comb(2 * N, N) * 2 * N) % MOD;
		for ( int i = 0 ; i < M ; i++ ) {
			if ( t[i] == 1 ) {
				long a1 = comb(x[i] + y[i], x[i]);
				long a2 = comb(2 * N - x[i] - y[i] - 1, N - x[i] - 1);
				ans = (ans - (a1 * a2) % MOD + MOD) % MOD;
			} else {
				long a1 = comb(x[i] + y[i], x[i]);
				long a2 = comb(2 * N - x[i] - y[i] - 1, N - x[i]);
				ans = (ans - (a1 * a2) % MOD + MOD) % MOD;
			}
		}

		System.out.println(ans);
	}
	
	static long comb(int n, int a) {
		long ret = frac[n];
		long x = modInv(frac[a], MOD);
		long y = modInv(frac[n - a], MOD);
		return (((ret * x) % MOD) * y) % MOD;
	}
	
	static long rec(int a) {
		if ( a <= 0 ) {
			frac[a] = 1;
			return 1;
		}
		
		long x = (((long) a) * ((long)rec(a - 1))) % MOD;
		frac[a] = x;
		return x;
	}
	
	static long[] xgcd(long a, long b) {
		long x0 = 1;
		long y0 = 0;
		long x1 = 0;
		long y1 = 1;
		while ( b != 0 ) {
			long q = a / b;
			long tmp = b;
			b = a % b;
			a = tmp;

			tmp = x1;
			x1 = x0 - q * x1;
			x0 = tmp;

			tmp = y1;
			y1 = y0 - q * y1;
			y0 = tmp;
		}
		return new long[] {a, x0, y0};
	}

	static long modInv(long a, long p) {
		long[] xgcd = xgcd(a, p);
		return (xgcd[1] + p) % p;
	}

}
0