結果

問題 No.140 みんなで旅行
ユーザー ぴろずぴろず
提出日時 2015-03-08 11:12:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 180 ms / 5,000 ms
コード長 1,164 bytes
コンパイル時間 2,067 ms
コンパイル使用メモリ 73,416 KB
実行使用メモリ 60,376 KB
最終ジャッジ日時 2023-09-06 21:29:37
合計ジャッジ時間 6,524 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,256 KB
testcase_01 AC 120 ms
55,768 KB
testcase_02 AC 127 ms
55,552 KB
testcase_03 AC 122 ms
55,744 KB
testcase_04 AC 123 ms
55,720 KB
testcase_05 AC 123 ms
55,668 KB
testcase_06 AC 122 ms
55,776 KB
testcase_07 AC 123 ms
55,432 KB
testcase_08 AC 122 ms
54,096 KB
testcase_09 AC 120 ms
55,412 KB
testcase_10 AC 123 ms
55,852 KB
testcase_11 AC 179 ms
60,008 KB
testcase_12 AC 129 ms
55,200 KB
testcase_13 AC 125 ms
55,572 KB
testcase_14 AC 180 ms
59,640 KB
testcase_15 AC 178 ms
60,080 KB
testcase_16 AC 147 ms
57,624 KB
testcase_17 AC 137 ms
56,016 KB
testcase_18 AC 178 ms
60,096 KB
testcase_19 AC 179 ms
60,376 KB
testcase_20 AC 134 ms
56,220 KB
testcase_21 AC 124 ms
55,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no140;

import java.util.Scanner;

public class Main {
	static final long MOD = 1_000_000_007;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[][] dp = new long[n+1][n+1];
		long[][] c = Mod.combinationArray(n, n, MOD);
		dp[0][0] = 1;
		long ans = 0;
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=i;j++) {
				dp[i][j] = (dp[i-1][j-1] + j * dp[i-1][j] % MOD) % MOD;
				ans = ans + c[n][i] * dp[i][j] % MOD * Mod.pow(j * (j-1) % MOD, n - i, MOD) % MOD;
				if (ans >= MOD) {
					ans -= MOD;
				}
			}
		}
		System.out.println(ans);
	}

}
class Mod {
	public static long[][] combinationArray(int maxN,int maxR,long mod) {
		long[][] c = new long[maxN+1][maxR+1];
		for(int i=0;i<=maxN;i++) {
			c[i][0] = 1;
			c[i][i] = 1;
		}
		for(int i=1;i<=maxN;i++) {
			for(int j=1;j<=i-1;j++) {
				c[i][j] = c[i-1][j-1] + c[i-1][j];
				if (c[i][j] >= mod) {
					c[i][j] -= mod;
				}
			}
		}
		return c;
	}
	public static long pow(long x,long n,long mod) {
		long res = 1;
		while(n > 0) {
			if ((n & 1) > 0) {
				res = (res * x) % mod;
			}
			x = (x * x) % mod;
			n/=2;
		}
		return res;
	}
}
0