Magic Master
题目描述
John is not only a magic master but also a shuffling master.
Famous though he is, he likes interacting with his fans by playing a game with his fantastic shuffling skills.
The game shows as follows:
He first shows a deck of pokers contains N cards indexed 1,2,…,N and all the cards are the same. He notes that the side contains numbers as the front side and the other side as the back. Later he selects one of his fans to choose a positive integer M that is not greater than 10. After that, he will shuffle the cards purposefully and put it on the desktop. Note that at this moment the front sides of the cards are facing to the desk.
Then, the selected fans should perform the following steps until there is no card on the desk.
-
Place the card on the top of the piles to John’s hand. If there are already some cards in his hand, put it on the top of them.
-
If there remain cards on the desk:
(a) Put a card from the top of the piles to the bottom of it.
(b) Execute the step (a) of M times.
© Go to step 1.
Next, it is time to witness the miracle.
John will continually overturn the cards on his hand top to bottom, and we will find that the cards are always in decreasing order.
One day, one of John’s fans, Tom, a smart JBer, understands the key to this magic. He turns to you and comments that the key to that magic is the shuffling John did previously. For any number M, he will turn the cards in a specific order after the shuffling. As you are not as smart as Tom, you should find the index of the Kth cards from the top of the piles after the shuffling.
Input
The first line contain a positive integer T(1 ≤ T ≤ 10) – the number of test cases.
For each test cases, the first line contain a positive integer N(1 ≤ N ≤ 40000000) , indicating the numberof cards.
The second line contain a positive integer M(1 ≤ M ≤ 10) – the number the fans select.
The third line contain an integer Q(1 ≤ Q ≤ 100) – indicating that there are Q questions.
Next, there are Q lines, each with an integer K(1 ≤ K ≤ N) – representing a query.
Output
For each query, you should output the index of the Kth cards from the top of the piles after the shuffling.
样例输入
1
5
1
2
2
3
样例输出
5
2
样例解释
Sample description - The indexs of the cards after the shuffling are: 1 5 2 4 3 (Left is top)
分析
直接用 队列
模拟
,跑得过 |l|ε=
代码
运行时间 : 2216ms | 运行内存 : 322856kB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
#define inf 1ll<<60
#define zero 1e-7
typedef long long ll;
const int N=4e7+5;
const int maxn=5e3+5;
const ll mod=1e9+7;
queue<int> que;
int v[N];
int main() {
int t, n, m, q, k;
scanf("%d", &t);
while(t--){
scanf("%d %d %d", &n, &m, &q);
while(!que.empty()) que.pop();
que.push(n);
for(int j=n-1; j>1; j--) {
que.push(j);
for(int c=1; c<=m; c++) {//m
int top=que.front();
que.pop();
que.push(top);
}
}
que.push(1);
int cnt=n;
while(!que.empty()) {
int top=que.front();
v[cnt--]=top;
que.pop();
//printf("%d\n", top);
}
while(q--) {
scanf("%d", &k);
printf("%d\n", v[k]);
}
}
return 0;
}