結果

問題 No.916 Encounter On A Tree
ユーザー もりをもりを
提出日時 2019-10-25 23:20:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,520 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 169,280 KB
実行使用メモリ 19,772 KB
最終ジャッジ日時 2023-10-11 09:37:43
合計ジャッジ時間 5,637 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
19,528 KB
testcase_01 AC 16 ms
19,524 KB
testcase_02 AC 16 ms
19,400 KB
testcase_03 AC 17 ms
19,468 KB
testcase_04 AC 16 ms
19,408 KB
testcase_05 WA -
testcase_06 AC 54 ms
19,452 KB
testcase_07 AC 16 ms
19,588 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 16 ms
19,404 KB
testcase_11 WA -
testcase_12 AC 131 ms
18,284 KB
testcase_13 WA -
testcase_14 AC 22 ms
19,452 KB
testcase_15 WA -
testcase_16 AC 17 ms
19,508 KB
testcase_17 WA -
testcase_18 AC 16 ms
19,452 KB
testcase_19 AC 16 ms
19,524 KB
testcase_20 AC 17 ms
19,404 KB
testcase_21 WA -
testcase_22 AC 16 ms
19,404 KB
testcase_23 WA -
testcase_24 AC 16 ms
19,560 KB
testcase_25 AC 16 ms
19,580 KB
testcase_26 AC 16 ms
19,452 KB
testcase_27 AC 16 ms
19,524 KB
testcase_28 AC 16 ms
19,468 KB
testcase_29 WA -
testcase_30 AC 16 ms
19,404 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 16 ms
19,404 KB
testcase_35 AC 16 ms
19,456 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 16 ms
19,460 KB
testcase_43 AC 16 ms
19,400 KB
testcase_44 AC 16 ms
19,412 KB
testcase_45 AC 16 ms
19,588 KB
testcase_46 AC 16 ms
19,408 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 16 ms
19,460 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 17 ms
19,588 KB
testcase_55 AC 16 ms
19,412 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
using ui64 = uint_fast64_t;
#define REP(i, n) for (i64 (i) = 0; (i) < (n); ++(i))
#define FOR(i, a, b) for (i64 (i) = (a); (i) < (b); ++(i))

constexpr int MOD = (int)1e9 + 7;
ui64 MOP(ui64 x, ui64 n, ui64 m = MOD) {
    ui64 r = 1;
    while (n > 0) {
        if (n & 1) (r *= x) %= m;
        (x *= x) %= m;
        n >>= 1;
    }
    return r;
}

int D, L, R, K;
vector<ui64> fact, dep;

ui64 dist(ui64 a, ui64 b) {
    vector<ui64> v1, v2;
    while (a > 0) {
        v1.emplace_back(a);
        a >>= 1;
    }
    while (b > 0) {
        v2.emplace_back(b);
        b >>= 1;
    }
    for (int i = 0; i < v1.size(); ++i) {
        for (int j = 0; j < v2.size(); ++j) {
            if (v1[i] == v2[j]) {
                return i + j;
            }
        }
    }
    return 0;
}

signed main() {

    cin >> D >> L >> R >> K;

    if (K > D * 2) {
        cout << -1 << endl;
        return 0;
    }
    
    dep.resize(D + 1);
    dep[0] = dep[1] = 1;
    REP(i, D - 1) dep[i + 2] = dep[i + 1] * 2;

    fact.resize(2100100);
    fact[0] = fact[1] = 1;
    REP(i, 2100108) fact[i + 2] = (fact[i + 1] * (i + 2)) % MOD;
    
    ui64 dl = 0, dr = 0;
    {
        int tmp = L;
        while (tmp > 0) {
            ++dl;
            tmp >>= 1;
        }
        tmp = R;
        while (tmp > 0) {
            ++dr;
            tmp >>= 1;
        }
    }

    ui64 res = 0;
    if (dl == dr) {
        if (K % 2 == 1) {
            cout << 0 << endl;
            return 0;
        }
        K >>= 1;
        res = dep[K];
        REP(i, K) (res <<= 1) %= MOD;
        (res *= dep[dl - K]) %= MOD;
        for (int i = 1; i <= D; ++i) {
            if (i == dl) {
                res *= fact[dep[i] - 2];
            } else {
                res *= fact[dep[i]];
            }
            res %= MOD;
        }
    } else {
        int dif = dr - dl;
        if (K - dif % 2 == 1) {
            cout << 0 << endl;
            return 0;
        }
        int k = (K - dif) >> 1;
        {
            int x = 1 << (dl - 1);
            for (int y = (1 << (dr - 1)); y < (1 << dr); ++y) {
                if (dist(x, y) == K) ++res;
            }
        }
        (res *= dep[dr]) %= MOD;
        for (int i = 1; i <= D; ++i) {
            if (i == dl or i == dr) {
                res *= fact[dep[i] - 1];
            } else {
                res *= fact[dep[i]];
            }
            res %= MOD;
        }
    }

    cout << res << endl;

}
0