結果

問題 No.391 CODING WAR
ユーザー ぴろず
提出日時 2016-07-08 22:45:32
言語 Java
(openjdk 23)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 1,775 bytes
コンパイル時間 2,362 ms
コンパイル使用メモリ 77,888 KB
実行使用メモリ 43,932 KB
最終ジャッジ日時 2024-10-13 06:12:05
合計ジャッジ時間 6,834 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

package no391;

import java.util.Scanner;

public class Main {
	/*
	 * 問題への割り当てf(x)
	 * 異なる全射の数え上げ
	 * ググった
	 * http://mathtrain.jp/zensya
	 */
	
	public static long MOD = 1000000007;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		int m = sc.nextInt();
		if (n < m) {
			System.out.println(0);
			return;
//			throw new RuntimeException("反則");
		}
		long ans = 0;
		long[] fact = Mod.factorialArray(m+1, MOD);
		long[] factInv = Mod.factorialInverseArray(m+1, MOD, Mod.inverseArray(m+1, MOD));
		for(int i=1;i<=m;i++) {
			long x = fact[m] * factInv[i] % MOD * factInv[m-i] % MOD * Mod.pow(i, n, MOD);
			ans = Mod.n(ans + ((m - i) % 2 == 0 ? 1 : -1) * x, MOD);
		}
		System.out.println(ans);
	}

}
class Mod {
	public static long n(long x,long mod) {
		x %= mod;
		if (x < 0) {
			x += mod;
		}
		return x;
	}
	public static long pow(long a,long n,long mod) {
		long res = 1;
		while(n > 0) {
			if ((n & 1) > 0) {
				res = (res * a) % mod;
			}
			a = (a * a) % mod;
			n/=2;
		}
		return res;
	}
	public static long[] inverseArray(int maxN,long modP) {
		long[] inv = new long[maxN+1];
		inv[1] = 1;
		for(int i=2;i<=maxN;i++) {
			inv[i] = modP - (modP / i) * inv[(int) (modP%i)] % modP;
		}
		return inv;
	}
	public static long[] factorialArray(int maxN,long mod) {
		long[] fact = new long[maxN+1];
		fact[0] = 1 % mod;
		for(int i=1;i<=maxN;i++) {
			fact[i] = fact[i-1] * i % mod;
		}
		return fact;
	}
	public static long[] factorialInverseArray(int maxN,long modP,long[] inverseArray) {
		long[] factInv = new long[maxN+1];
		factInv[0] = 1;
		for(int i=1;i<=maxN;i++) {
			factInv[i] = factInv[i-1] * inverseArray[i] % modP;
		}
		return factInv;
	}

}
0