跳到正文

题解

Alice's Adventures in Permuting

MrPython约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月5日
更新
2026年6月15日
篇幅
约 2 分钟
题目
CF2028B

版本

  1. v1当前

怎么搞的,为什么好多大佬都在这里爆炸了?

对于 b>0b>0,其最终效果一定是将所有不小于 nn 的数变成在 00 到 n−1n-1 中没有出现的数。应用乌姆·尼克算法二分找到有多少个数小于 nn,剩下的数都要操作各一遍。

b=0b=0 需要稍微处理一下:

  • c≥nc\ge n 时,初始数组内的所有数都大于等于 nn,需要各操作一遍,共 nn 次。
  • c=n−1c= n-1 时,第 00 到 n−2n-2 个数分别修改为 00 到 n−2n-2,第 n−1n-1 项本身为 n−1n-1 无需修改,共 n−1n-1 次;
  • c=n−2c= n-2 时,第 00 到 n−3n-3 个数分别修改为 00 到 n−3n-3,第 n−2n-2 项被修改为 n−1n-1,第 n−1n-1 项已经为 n−2n-2 不变,共 n−1n-1 次;
  • c<n−2c< n-2 时,我们会一直重复操作那个最大的元素,永远无法到达答案,无解。
C++
#include <algorithm>
#include <iostream>
#include <ranges>
using namespace std;
istream& fin = cin;
ostream& fout = cout;
using ui = unsigned int;
using uli = unsigned long long int;
using li = long long int;
using ulli = __uint128_t;
int main(void) {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  size_t T;
  fin >> T;
  while (T--) {
    uli n, b, c;
    fin >> n >> b >> c;
    if (b == 0) {
      if (c + 2 < n)
        fout << "-1\n";
      else
        fout << (c == n - 2 ? n - 1 : c == n - 1 ? n - 1 : n) << '\n';
      continue;
    }
    auto view =
        ranges::views::iota(0ull, n) |
        ranges::views::transform([b, c](uli x) { return (ulli)b * x + c; });
    fout << (n - uli(ranges::lower_bound(view, n) - view.begin())) << '\n';
  }
  return 0;
}
原文
存档评论1 条抓取于