結果

問題 No.911 ラッキーソート
ユーザー finefine
提出日時 2019-10-19 00:17:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 2,634 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 173,800 KB
実行使用メモリ 11,012 KB
最終ジャッジ日時 2023-09-08 02:59:45
合計ジャッジ時間 7,417 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 157 ms
11,012 KB
testcase_14 AC 159 ms
10,844 KB
testcase_15 AC 158 ms
10,964 KB
testcase_16 AC 161 ms
10,968 KB
testcase_17 AC 162 ms
10,900 KB
testcase_18 AC 161 ms
10,852 KB
testcase_19 AC 165 ms
10,700 KB
testcase_20 AC 156 ms
10,888 KB
testcase_21 AC 146 ms
10,772 KB
testcase_22 AC 137 ms
10,792 KB
testcase_23 AC 138 ms
10,684 KB
testcase_24 AC 125 ms
10,716 KB
testcase_25 AC 72 ms
7,148 KB
testcase_26 AC 66 ms
6,780 KB
testcase_27 AC 27 ms
4,880 KB
testcase_28 AC 13 ms
4,376 KB
testcase_29 AC 5 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,388 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 156 ms
10,984 KB
testcase_39 AC 136 ms
10,788 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 154 ms
11,004 KB
testcase_42 AC 59 ms
7,048 KB
testcase_43 AC 160 ms
10,832 KB
testcase_44 AC 39 ms
4,676 KB
testcase_45 AC 40 ms
4,676 KB
testcase_46 AC 38 ms
4,592 KB
testcase_47 AC 37 ms
4,672 KB
testcase_48 AC 38 ms
4,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, int>;

int calcBoundary(int bit, int l, int r, vector<ll>& a) {
    ll mask = (1LL << bit);
    int idx = 0;
    bool pf = (a[l] & mask);
    for (int i = l; i < r; i++) {
        bool f = (a[i] & mask);
        if (pf != f) {
            if (idx == 0) {
                pf = f;
                idx = i;
            } else {
                return -1;
            }
        } 
    }
    return idx;
}

ll calc_less(int bit, vector<ll>& a, vector<P>& v) {
    if (bit < 0) return 1;

    ll res = 1;
    for (int i = bit; i >= 0; i--) {
        ll mask = (1LL << i);
        vector<P> nv;
        bool f01 = false, f10 = false;
        for (P& p : v) {
            int b = calcBoundary(i, p.first, p.second, a);
            if (b == -1) return 0;

            if (b == 0) {
                nv.push_back(p);
            } else {
                nv.emplace_back(p.first, b);
                nv.emplace_back(b, p.second);
                if (a[b] & mask) f01 = true;
                else f10 = true;
            }
        }

        if (f01 && f10) return 0;
        if (!f10 && !f01) {
            res <<= 1;
        }
        v = move(nv);
    }
    cerr << res << ": \n";
    return res;
}

ll solve(ll lim, int n, vector<ll>& a) {
    if (lim < 0) return 0;
    vector<P> v;
    v.emplace_back(0, n);
    vector<ll> dp(2, 0);
    dp[true] = 1;
    for (int i = 60; i >= 0; i--) {
        vector<ll> ndp(2, 0);
        ll mask = (1LL << i);
        vector<P> nv;
        bool f01 = false, f10 = false;

        for (P& p : v) {
            int b = calcBoundary(i, p.first, p.second, a);
            if (b == -1) return 0;

            if (b == 0) {
                nv.push_back(p);
            } else {
                nv.emplace_back(p.first, b);
                nv.emplace_back(b, p.second);
                if (a[b] & mask) f01 = true;
                else f10 = true;
            }
        }

        if (f01 && f10) return 0;
        if (!f10) {
            ndp[false] += dp[false];
            if (lim & mask) ndp[false] += dp[true];
            else ndp[true] += dp[true];
        }
        if (!f01) {
            ndp[false] += dp[false];
            if (lim & mask) ndp[true] += dp[true];
        }
        dp = move(ndp);
        v = move(nv);
    }
    return dp[false] + dp[true];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    ll L, R;
    cin >> n >> L >> R;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    cout << solve(R, n, a) - solve(L - 1, n, a) << "\n";
    return 0;
}
0