結果

問題 No.758 VVVVV
ユーザー 37zigen37zigen
提出日時 2019-01-06 18:24:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 847 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 1,923 ms
コンパイル使用メモリ 77,676 KB
実行使用メモリ 69,348 KB
最終ジャッジ日時 2024-04-22 03:06:02
合計ジャッジ時間 14,827 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
52,736 KB
testcase_01 AC 104 ms
53,048 KB
testcase_02 AC 111 ms
54,008 KB
testcase_03 AC 816 ms
69,144 KB
testcase_04 AC 571 ms
59,404 KB
testcase_05 AC 474 ms
59,304 KB
testcase_06 AC 465 ms
59,144 KB
testcase_07 AC 847 ms
68,936 KB
testcase_08 AC 533 ms
59,532 KB
testcase_09 AC 368 ms
59,120 KB
testcase_10 AC 491 ms
59,260 KB
testcase_11 AC 818 ms
69,348 KB
testcase_12 AC 470 ms
59,204 KB
testcase_13 AC 501 ms
59,224 KB
testcase_14 AC 388 ms
58,884 KB
testcase_15 AC 319 ms
58,856 KB
testcase_16 AC 364 ms
59,292 KB
testcase_17 AC 755 ms
69,176 KB
testcase_18 AC 565 ms
59,236 KB
testcase_19 AC 799 ms
69,056 KB
testcase_20 AC 441 ms
59,448 KB
testcase_21 AC 450 ms
59,096 KB
testcase_22 AC 830 ms
69,172 KB
testcase_23 AC 116 ms
53,756 KB
testcase_24 AC 118 ms
54,156 KB
testcase_25 AC 111 ms
52,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main{

    final long mo=(long)1e9+7;
    
    public static void main(String[] args){
	    new Main().run();
    }

    void run(){
    	Scanner sc=new Scanner(System.in);
    	int N=sc.nextInt();
    	int[] deg=new int[N];
    	for(int i=0;i<N-1;++i){
    	    int a=sc.nextInt();
    	    int b=sc.nextInt();
    	    --a;
    	    --b;
    	    ++deg[a];
    	    ++deg[b];
    	}
    
    	if(N==1){
    	    System.out.println(1);
    	    return;
    	}
    	
    	int n=N-1;
    	int m=0;
    	for(int i=1;i<N;++i){
    	    if(deg[i]==1)
    		++m;
    	}
    	System.out.println(narayana(n,m));
    }

    long narayana(long n,long k){
	    return inv(k)*comb(n,k-1)%mo*comb(n-1,k-1)%mo;
    }

    long comb(long n,long k){
    	long ret=1;
    	for(long i=0;i<k;++i){
    	    ret=ret*(n-i)%mo*inv(i+1)%mo;
    	}
    	return ret;
    }

    long inv(long a){
	    return pow(a,mo-2);
    }

    long pow(long a,long n){
    	long ret=1;
    	for(;n>0;n>>=1,a=a*a%mo){
    	    if(n%2==1)ret=ret*a%mo;
    	}
    	return ret;
    }
}
0