Magic Master (The 2019 Asia Nanchang First Round Online Programming Contest)

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 NN cards indexed 1,2,\dots ,N1,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 MM that is not greater than 1010. 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. 

  1. 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. 
  2. 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 (aa) of MM times.

(c) Go to step 11.

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 MM, 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 KKth cards from the top of the piles after the shuffling.

Input

The first line contain a positive integer T (1\le T \le10)T(1T10) – the number of test cases.

For each test cases, the first line contain a positive integer N (1 \le N \le 40000000)N(1N40000000) , indicating the numberof cards.

The second line contain a positive integer M (1\le M \le 10)M(1M10) – the number the fans select.

The third line contain an integer Q (1 \le Q \le 100)Q(1Q100) – indicating that there are QQ questions.

Next, there are QQ lines, each with an integer K (1 \le K \le N)K(1KN) – representing a query.

Output

For each query, you should output the index of the KKth 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~31 5 2 4 3 (Left is top)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
int ans[40000000];
queue<int>q;int x;
int main()
{
    ll n,m;
    ll t;scanf("%lld",&t);
    while(t--)
    {scanf("%lld%lld",&n,&m);
        while(!q.empty())
            q.pop();
        for(int i=n;i>1;i--)
        {
            q.push(i);
            for(int j=1;j<=m;j++)
            {
                 ll k=q.front();
            q.pop();
            q.push(k);
            }
        }
        ans[1]=1;
        for(int i=n;i>1;i--)
        {
            ans[i]=q.front();
            //cout<<ans[i]<<' ';
            q.pop();
        }
        int ff;scanf("%d",&ff);
        for(int i=1;i<=ff;i++)
        {

            scanf("%d",&x);
            printf("%d\n",ans[x]);
        }
    }
}

 

posted @ 2019-09-08 22:18  __MEET  阅读(358)  评论(0编辑  收藏  举报