結果

問題 No.117 組み合わせの数
ユーザー kuuso1kuuso1
提出日時 2015-01-05 00:31:55
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 816 ms / 5,000 ms
コード長 2,397 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 104,396 KB
実行使用メモリ 48,220 KB
最終ジャッジ日時 2023-09-03 21:55:08
合計ジャッジ時間 4,536 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 816 ms
48,220 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
 
class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		for(int t=0;t<T;t++){
			char typ=In[t][0];
			var d=Array.ConvertAll(In[t].Substring(2,In[t].Length-3).Split(','),e=>long.Parse(e));
			long N=d[0];long K=d[1];
			if(typ=='C'){
				Console.WriteLine(cal_nCrMod(N,K));
				continue;
			}
			if(typ=='P'){
				long x=cal_nCrMod(N,K)*FactorialMod(K);
				Console.WriteLine(x%mod);
				continue;
			}
			if(typ=='H'){
				if(N==0 && K==0){
					Console.WriteLine(1);
					continue;
				}
				Console.WriteLine(cal_nCrMod(N+K-1,K));
				continue;
			}
		
		}
	}
	
	long Max;
	long mod;
	String[] In;
	int T;
	public Sol(){
		mod=(long)(1e9+7);
		Max=(int)3e6;
		FM=new long[Max];
		FM[0]=1;
		for(int i=1;i<Max;i++){
			FM[i]=(FM[i-1]*i)%mod;
		}
		
		T=ri();
		In=new String[T];
		for(int i=0;i<T;i++)In[i]=rs();
	}

	
	long cal_nCrMod(long n,long r){
		if(n<r)return 0;
		if(n<0)return 0;
		if(r<0)return 0;
		if(n==0||r==n||r==0)return 1;
		long a=FactorialMod(n);
		long b=InvMod(FactorialMod(r));
		long c=InvMod(FactorialMod(n-r));
		long ans=(((a*b)%mod)*c)%mod;
		return ans;
	}
	
	long[] FM;
	long FactorialMod(long n){
		/*
		if(n<2)return 1;
		long ret=n*FactorialMod(n-1);
		ret%=mod;
		return ret;
		*/
		return FM[(int)n];
	}
	
	long InvMod(long n){
		if(n>mod)n%=mod;
		if(n==0)return 0;
		//(mod-2)乗を返す
		List<long> L=new List<long>();
		
		int idx=0;
		while(mod>(0x1<<idx))idx++;
		long[] Table=new long[idx];
		Table[0]=n;
		for(int i=1;i<idx;i++){
			Table[i]=Table[i-1]*Table[i-1];
			Table[i]%=mod;
		}
		
		
		long ret=1;
		for(int i=0;i<idx;i++){
			if(( (mod-2)&(0x1<<i)) >0){
				ret*=Table[i];
				ret%=mod;
			}
		}
		
		return ret;
		
		
	}




	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(){return Console.ReadLine().Split(' ');}
	static int[] ria(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>int.Parse(e));}
	static long[] rla(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>long.Parse(e));}
	static double[] rda(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>double.Parse(e));}
}
0