結果

問題 No.391 CODING WAR
ユーザー fjafjafjafjafjafja
提出日時 2017-09-23 01:07:42
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 1,804 bytes
コンパイル時間 3,644 ms
コンパイル使用メモリ 77,476 KB
実行使用メモリ 833,060 KB
最終ジャッジ日時 2024-04-26 03:57:26
合計ジャッジ時間 6,328 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
54,220 KB
testcase_01 MLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class N391
{
	static long beki=(long)Math.pow(10, 9)+7;
	static long ans=0,n;
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		n=sc.nextLong();
		int end=0;
		long m=sc.nextLong();
		long[] h=new long[(int)m];
		long ans=0;

		if(n<m){System.out.println(0);end++;}
		else if(n==m){System.out.println(factrial(n));end++;}
		else
		{
			long rem=m-n;//振り分ける数
			hchange(h,rem);
		}
		if(end==0)
		{
			System.out.println(ans);
		}
	}

	static void hchange(long[] h,long rem)
	{
		if(rem==0)
		{
			if(issyoujun(h)){ans+=hairetu(h)*hairetunarabe(h,n);
}
			return;
		}
		for(int i=0;i<h.length;i++)
		{
			h[i]++;
			hchange(h,rem-1);
		}
	}

	static boolean issyoujun(long[] h)//配列が昇順かを判定
	{
		long pre=-1;
		for(int i=0;i<h.length;i++)
		{
			if(pre>h[i]){return false;}
			pre=h[i];
		}
		return true;
	}
	static void syokika(long[] h)
	{
		for(int i=0;i<h.length;i++)
		{
			h[i]=1;
		}
	}
	static long hairetunarabe(long[] h,long n)
	{
		long ans=1;
		long l=h.length;
		for(int i=0;i<h.length;i++)
		{
			ans=(ans*ncm(n,h[i]))%beki;
			n-=h[i];
		}
		return ans;
	}

	static long hairetu(long[] h)//昇順に並んだ配列hの並べ方数を返す。
	{
		long pre=0,cnt=1,under=1,over=1;
		for(int i=0;i<h.length;i++)
		{
			over*=(i+1)%beki;
			if(pre==h[i]){cnt++;}
			else{under=under*factrial(cnt)%beki;cnt=1;}
			pre=h[i];
		}
		return over/under;
	}



	static long factrial(long n)//nの階乗を返す。
	{
		long ans=1;
		for(int i=1;i<=n;i++)
		{
			ans*=i;
		}
		return ans;
	}

	static long ncm(long n,long m)//組み合わせnCmを返す。
	{
		long ans=1;
		for(long i=0;i<m;i++)
		{
			ans=ans*(n-i)%beki;
		}
		for(int i=(int)m;i>0;i--)
		{
			ans/=i;
		}
		return ans;
	}

}
0