Codeforces Round #469 (Div. 2) D. A Leapfrog in the Array【规律】

D. A Leapfrog in the Array
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 ≤ 10181 ≤ 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
Copy
4 3
2
3
4
output
Copy
3
2
4
input
Copy
13 4
10
5
4
8
output
Copy
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].


1 1
1 2
1 3 2
1 3 2 4
1 5 2 4 3
1 4 2 6 3 5
1 6 2 5 3 7 4
1 5 2 7 3 6 4 8
1 9 2 6 3 8 4 7 5
1 6 2 10 3 7 4 9 5 8

规律题是真的让我望尘莫及。

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
using namespace std;
typedef long long ll;

const int N=2e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

int main(void){
    ll n,q,p,t;
    cin>>n>>q;
    t=n;
    while(q--){
        scanf("%I64d",&p);
//        cout <<"p="<<p<<endl;
        if(p%2==1){
            printf("%lld\n",p/2+1);
        }
        else{
            ll nn=n=t;
            while(p%2==0){
                ll cnt=p/2;
                p-=cnt*2;
                n-=cnt;
                if(p==0)    p+=n;
            }
            printf("%I64d\n",p/2+1 + nn-n);
        }
    }


    return 0;
}


猜你喜欢

转载自blog.csdn.net/haipai1998/article/details/80021265