Codeforces Round #469 (Div. 2) D. A Leapfrog in the Array(思维+math)

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-n个数字,每个数字的位置是2*n-1,每次让最后一个数字向前移动,问当所有的格子都被填满之后,第op个位置上是哪个数字?

题解:比较有趣的思维题,代码实现很简单,这就是CF呀。首先明确一点就是奇数位置上面的数字不会移动,那么对于刚移动完的偶数位置上的数字它的左边一定有indx/2个数字,indx是这个偶数位的位置,那么右边就有n-indx/2-1个数字,那么它上一次跳跃就是从indx+(n-indx/2)的位置移动过来的,如此这样对于每个位置逆推回去就可以得到最终的答案。(复杂度不会算,180ms过,复杂度应该不会太高)。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<time.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
typedef long long ll;
using namespace std;
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

int main()
{
    ll n,q;
    n=read();
    q=read();
    while(q--)
    {
        ll op;
        op=read();

        while((op&1)==0) op+=(n-op/2);

        printf("%I64d\n",op/2+1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/80061691