結果

問題 No.758 VVVVV
ユーザー 37zigen37zigen
提出日時 2019-01-06 18:24:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 788 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 74,100 KB
実行使用メモリ 65,124 KB
最終ジャッジ日時 2023-08-04 04:46:17
合計ジャッジ時間 15,647 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,812 KB
testcase_01 AC 124 ms
55,776 KB
testcase_02 AC 125 ms
55,772 KB
testcase_03 AC 788 ms
65,124 KB
testcase_04 AC 532 ms
60,680 KB
testcase_05 AC 468 ms
60,852 KB
testcase_06 AC 498 ms
60,660 KB
testcase_07 AC 736 ms
64,612 KB
testcase_08 AC 569 ms
60,876 KB
testcase_09 AC 409 ms
60,292 KB
testcase_10 AC 517 ms
60,192 KB
testcase_11 AC 777 ms
64,500 KB
testcase_12 AC 448 ms
60,652 KB
testcase_13 AC 523 ms
60,672 KB
testcase_14 AC 379 ms
60,184 KB
testcase_15 AC 333 ms
60,220 KB
testcase_16 AC 374 ms
60,428 KB
testcase_17 AC 723 ms
64,420 KB
testcase_18 AC 545 ms
60,540 KB
testcase_19 AC 785 ms
64,888 KB
testcase_20 AC 463 ms
60,572 KB
testcase_21 AC 471 ms
60,596 KB
testcase_22 AC 780 ms
64,512 KB
testcase_23 AC 123 ms
55,792 KB
testcase_24 AC 125 ms
55,884 KB
testcase_25 AC 131 ms
55,468 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