题意:一排n个人,编号从1到n,从1开始每隔k选一个人,选完后,剩下的人重新排列,继续选,直到选完。
思路:为了方便我们编号从0开始,设此时编号i,如果i被k整除,那么i在第一轮被选出,否则,i可以用i - i / k - 1代替。求出所有人在第几轮第几个选出,然后排个序输出就行了。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include<vector>
#include<cmath>
#include<set>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 31e6 + 10;
const int maxt = 100200;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
typedef long long ll;
struct Node{
int x, y;
int id;
bool operator<(const Node& rhs) const{
return x < rhs.x || x == rhs.x && y < rhs.y;
}
}a[maxn];
int main(){
int T;
scanf("%d", &T);
while(T--){
int n, k, q;
scanf("%d%d%d", &n, &k, &q);
int cnt = 1;
for(int i = 0; i < n; ++i){
if(i % k == 0){
a[i].x = 1;
a[i].y = cnt++;
a[i].id = i + 1;
}
else{
a[i].x = a[i - i / k - 1].x + 1;
a[i].y = a[i - i / k - 1].y;
a[i].id = i + 1;
}
}
sort(a, a + n);
while(q--){
int x;
scanf("%d", &x);
printf("%d\n", a[x - 1].id);
}
}
}