結果

問題 No.758 VVVVV
ユーザー nasadigitalnasadigital
提出日時 2018-12-08 10:03:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,978 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 108,984 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-04 04:40:40
合計ジャッジ時間 2,345 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 22 ms
4,376 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 22 ms
4,376 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 23 ms
4,376 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 19 ms
4,376 KB
testcase_18 AC 11 ms
4,376 KB
testcase_19 AC 23 ms
4,376 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 8 ms
4,380 KB
testcase_22 AC 23 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <istream>
#include <map>
#include <numeric>
#include <ostream>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define MOD 1000000007


long long comb(int n, int k, int mo);
long long fast_pow(long long a, int n, int mo = 0);


using namespace std;


class Solution {
 public:
  void solve(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    int ar[n];
    memset(ar, 0, sizeof(ar));
    int a, b;
    for (int ctr1 = 1; ctr1 < n; ++ctr1)
      in >> a >> b, ++ar[a - 1], ++ar[b - 1];
    int m = 0;
    for (int ctr1 = 1; ctr1 < n; ++ctr1)
      m += ar[ctr1] == 1;
    if (n == 1) {
      out << 1;
      return;
    }
    out << ((comb(n - 1, m, MOD) * comb(n - 1, m - 1, MOD) % MOD) *
           fast_pow(n - 1, MOD - 2, MOD)) % MOD;
  }
};

void solve(std::istream& in, std::ostream& out)
{
  out << std::setprecision(12);
  Solution solution;
  solution.solve(in, out);
}
// Powered by caide (code generator, tester, and library code inliner)


#include <fstream>
#include <iostream>


int main() {
  
  ios_base::sync_with_stdio(false);
  cin.tie(0);


  istream& in = cin;


  ostream& out = cout;

  solve(in, out);
  return 0;
}


long long comb(int n, int k, int mo) {
  if (n - k < k)
    k = n - k;
  int inv[k + 1];
  inv[0] = inv[1] = 1;
  for (long long ctr1 = 2; ctr1 <= k; ++ctr1)
    inv[ctr1] = inv[mo % ctr1] * (mo - mo / ctr1) % mo;
  long long rez = 1;
  for (int ctr1 = 2; ctr1 <= k; ++ctr1)
    rez = (rez * inv[ctr1]) % mo;
  for (int ctr1 = 0; ctr1 < k; ++ctr1)
    rez = (rez * (n - ctr1)) % mo;
  return rez;
}

long long fast_pow(long long a, int n, int mo) {
  if (n == 0)
    return 1;
  if (mo == 0)
    return ((n & 1) ? a : 1) * fast_pow(a * a, n >> 1, mo);
  return (((n & 1) ? a : 1) * fast_pow((a * a) % mo, n >> 1, mo)) % mo;
}

0