結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー lavoxlavox
提出日時 2021-07-09 22:11:42
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,390 ms / 2,000 ms
コード長 1,765 bytes
コンパイル時間 3,497 ms
コンパイル使用メモリ 78,032 KB
実行使用メモリ 95,828 KB
最終ジャッジ日時 2024-07-01 16:42:57
合計ジャッジ時間 20,753 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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