Files
python/GESP/Level6/P9573_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

50 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, p;
queue<int> q[N]; // 模p的余数组成的队列数组
set<int> s; // 集合?
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> p;
// 如果p太大就不用算了,因为即使 n+ n-1 也无法构建出一个p,这个可以根据测试用例去想一个特判
if (p >= 2 * n) {
for (int i = 1; i <= n; i++)
cout << i << " ";
cout << endl;
continue;
}
for (int i = 1; i <= n; i++) // 按 模p分组
q[i % p].push(i);
for (int i = 0; i < p; i++)
if (q[i].size())
s.insert(i); // 队列非空才加入, s里装的是 模p的余数
int x = 0;
while (q[x].empty())
x++;
int y = q[x].front();
q[x].pop();
cout << y << " ";
for (int i = 1; i <= n; i++) { // 还有n-1个数字需要输出
int f = (p - x % p) % p; // 防止x%p=0的特殊情况
if (q[f].empty())
s.erase(f), f = *s.begin(); // 没了就删除f换成任意一个非空队列
x = q[f].front();
q[f].pop();
cout << x << " ";
if (q[f].empty())
s.erase(f);
}
cout << endl;
}
return 0;
}