A Leapfrog in the Array (CodeForces - 949B )

Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.

Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:

You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.

Input

The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.

Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.

Output

For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.

Examples

Input

4 3
2
3
4

Output

3
2
4

Input

13 4
10
5
4
8

Output

13
3
8
9

Note

The first example is shown in the picture.

In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].

题意:这道题的话,是给你n个数,但是这n个数储存在2*n-1的单元里,奇数单元格存数,偶数单元格里不存数。然后,每个数从最后一个开始会往前移动,若这个数前面没有数,则移动一个单元格,若有数,则可以越过这个数。具体怎么操作,题里的图给的很清楚,然后给你q次查询,每次查询第几个单元格里的数是多少。依次输出。

思路:推导一下这个转变的过程,你会发现,要查询的位置是奇数的时候,直接查询的数就是(a+1)/2。

若是偶数的话,暴力打表一下,你会发现偶数位置上的数是上一行的a-2的数,然后每次当查询是偶数的时候,就算出(n-a+(a+1)/2)就行了。

AC代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
typedef long long ll;
const int maxx=10010;
const int inf=0x3f3f3f3f;
using namespace std;
int main()
{
    ll n,q,a,m;
    cin>>n>>q;
    while(q--)
    {
        cin>>a;
        if(a%2!=0)
        {
            cout<<(a+1)/2<<endl;
            continue;
        }
        m=n;
        while(a%2==0)
        {
            a=m-a/2;
            m=a;
        }
        cout<<n-a+(a+1)/2<<endl;
    }
    return 0;
}
发布了204 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43846139/article/details/103464204