The 2019 Asia Nanchang First Round Online Programming Contest 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 NNcards 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(1≤T≤10) – the number of test cases.

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

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

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

Next, there are QQ lines, each with an integer K (1 \le K \le N)K(1≤K≤N) – 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)

规律很好找从空着的格子里每隔M个按顺序放就行了,这个题一开始暴力做的,尝试性的交了一发WA掉。然后就一直在想用什么样的数据结构才能把那些已经填了的数给跳过。提解说新开一个数组来记录指针。一开始没看明白把他给理解成链表了样例虽然过但是提交的时候老是数组超界。后来发现他就只是单纯的单链表并不是双向的所以一个数组记录它所指向的下一个下标就行了。

#include <bits/stdc++.h>
using namespace std;
int a[40009000];
int b[40000900];
int main()
{
    int t,n,m;
    cin>>t;
    while(t--)
    {

        cin>>n>>m;
        a[0]=1;
        int j=0;
        int o=j;
        int ans=0;
        for(int i=1;i<n-1;i++)
            b[i]=i+1;
        b[n-1]=1;
        b[0]=1;
        for(int i=1; i<n; i++)
        {
            ans=0;
            for(; ans<=m; o=j,j=b[j])
            {
                ans++;
            }
            a[j]=i+1;
            b[o]=b[j];
        }
        int s;
        cin>>s;
        for(int i=1; i<=s; i++)
        {
            int k;
            cin>>k;
            cout<<a[k-1]<<endl;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值