結果

問題 No.911 ラッキーソート
ユーザー 0w1
提出日時 2019-11-28 23:28:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,189 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 201,152 KB
最終ジャッジ日時 2025-01-08 05:42:18
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);

  int N;
  int64_t L, R;
  cin >> N >> L >> R;

  vector<int64_t> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  auto calc = [&](int64_t ub, bool inc) -> int64_t {
    vector<int> must(61, -1);
    for (int i = 0; i + 1 < N; ++i) {
      for (int j = 60; ~j; --j) {
        int x = A[i] >> j & 1;
        int y = A[i + 1] >> j & 1;
        if (x == y) continue;
        if (must[60 - j] == -1) must[60 - j] = y < x;
        if (must[60 - j] != (y < x)) return 0;
        break;
      }
    }
    vector<vector<int64_t>> dp(62, vector<int64_t>(2));
    dp[0][0] = 1;
    for (int i = 0; i < 61; ++i) {
      vector<int> choices;
      if (must[i] == -1) choices = { 0, 1 };
      else choices = { must[i] };
      for (int l = 0; l < 2; ++l) {
        int v = ub >> 60 - i & 1;
        for (int d : choices) {
          if (!l && v < d) continue;
          dp[i + 1][l | d < v] += dp[i][l];
        }
      }
    }
    int64_t res = dp[61][1];
    if (inc) res += dp[61][0];
    return res;
  };

  int64_t ans = calc(R, true) - calc(L, false);
  cout << ans << endl;

  return 0;
}
0