結果

問題 No.916 Encounter On A Tree
ユーザー kk
提出日時 2021-02-18 16:09:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,490 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 200,760 KB
実行使用メモリ 11,424 KB
最終ジャッジ日時 2023-10-13 00:16:24
合計ジャッジ時間 5,192 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
11,272 KB
testcase_01 AC 10 ms
11,404 KB
testcase_02 AC 10 ms
11,152 KB
testcase_03 AC 10 ms
11,156 KB
testcase_04 AC 11 ms
11,204 KB
testcase_05 AC 10 ms
11,224 KB
testcase_06 AC 10 ms
11,204 KB
testcase_07 AC 10 ms
11,288 KB
testcase_08 AC 11 ms
11,204 KB
testcase_09 AC 11 ms
11,180 KB
testcase_10 AC 10 ms
11,288 KB
testcase_11 AC 11 ms
11,152 KB
testcase_12 AC 11 ms
11,208 KB
testcase_13 AC 11 ms
11,124 KB
testcase_14 AC 10 ms
11,100 KB
testcase_15 AC 11 ms
11,424 KB
testcase_16 AC 11 ms
11,152 KB
testcase_17 AC 11 ms
11,108 KB
testcase_18 AC 10 ms
11,340 KB
testcase_19 AC 10 ms
11,336 KB
testcase_20 AC 10 ms
11,100 KB
testcase_21 AC 10 ms
11,108 KB
testcase_22 AC 10 ms
11,396 KB
testcase_23 AC 10 ms
11,332 KB
testcase_24 AC 10 ms
11,224 KB
testcase_25 AC 10 ms
11,348 KB
testcase_26 AC 11 ms
11,156 KB
testcase_27 AC 10 ms
11,272 KB
testcase_28 AC 10 ms
11,176 KB
testcase_29 AC 10 ms
11,172 KB
testcase_30 AC 10 ms
11,104 KB
testcase_31 AC 10 ms
11,104 KB
testcase_32 AC 10 ms
10,132 KB
testcase_33 AC 11 ms
11,276 KB
testcase_34 AC 10 ms
11,116 KB
testcase_35 AC 10 ms
11,108 KB
testcase_36 AC 10 ms
11,344 KB
testcase_37 AC 11 ms
11,332 KB
testcase_38 AC 10 ms
11,400 KB
testcase_39 AC 10 ms
11,116 KB
testcase_40 AC 11 ms
11,112 KB
testcase_41 AC 10 ms
11,108 KB
testcase_42 AC 10 ms
11,156 KB
testcase_43 AC 10 ms
11,348 KB
testcase_44 AC 10 ms
11,104 KB
testcase_45 AC 10 ms
11,208 KB
testcase_46 AC 10 ms
11,292 KB
testcase_47 AC 10 ms
11,272 KB
testcase_48 AC 10 ms
11,332 KB
testcase_49 AC 10 ms
11,100 KB
testcase_50 AC 10 ms
11,316 KB
testcase_51 AC 10 ms
11,404 KB
testcase_52 AC 11 ms
11,136 KB
testcase_53 AC 11 ms
11,156 KB
testcase_54 AC 10 ms
11,424 KB
testcase_55 AC 11 ms
11,172 KB
testcase_56 AC 11 ms
11,276 KB
testcase_57 AC 10 ms
11,116 KB
testcase_58 AC 10 ms
11,348 KB
testcase_59 AC 10 ms
11,340 KB
testcase_60 AC 10 ms
11,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1e9 + 7;

int depth(int v) {
  int d = 0;
  while (v > 0) {
    ++d;
    v /= 2;
  }
  return d;
}

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

  vector<long long> f(1<<20);
  f[0] = 1;
  for (int i = 1; i < 1<<20; i++)
    f[i] = f[i-1] * i % MOD;
  
  int d, l, r, k;
  cin >> d >> l >> r >> k;

  // r の方が深いところにある
  if (depth(l) > depth(r))
    swap(l, r);

  int dl = depth(l), dr = depth(r);
  long long ret = 1;
  if (dl == dr) {
    for (int i = 1; i <= d; i++) {
      if (i != dl) {
        ret *= f[1<<(i-1)];
        ret %= MOD;
      } else {
        if (k % 2 || k < 2 || k > dl + dr - 2)
          ret = 0;
        else {
          int ptr = 1<<(i-1);
          ret *= ptr;
          ret %= MOD;
          ret *= 1<<(k / 2 - 1);
          ret %= MOD;
          ret *= f[ptr - 2];
          ret %= MOD;
        }
      }
    }
  } else {
    for (int i = 1; i <= d; i++) {
      if (i != dr) {
        ret *= f[1<<(i-1)];
        ret %= MOD;
      } else {
        int tmp = k - (dr - dl);
        if (tmp < 0 || k > dl + dr - 2 || tmp % 2)
          ret = 0;
        else {
          int base = 1<<(dr - dl);
          for (int j = 0; j < tmp/2-1; j++)
            base *= 2;
          ret *= base;
          ret %= MOD;
          int ptr = 1<<(i-1);
          ret *= f[ptr - 1];
          ret %= MOD;
        }
      }
    }
  }
  cout << ret << endl;

  return 0;
}
0