Codeforces 950D A Leapfrog in the Array

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:

Codeforces 950D A Leapfrog in the Array_第1张图片

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个数,然后每个数i 在2*i-1 的位置,问你进行题目给的操作(看图),然后有m个查询,问你那个位置的数是什么?



这题给我的思考提供了一个全新的思考位置,先讲讲我是怎么样思考的。

一开始我是觉得这是找规律的题然后我就找了以下n的规律

n=1 1

n=2 1 2

n=3 1 3 2

n=4 1 3 2 4

n=5 1 5 2 4 3

n=6 1 4 2 6 3 5

n=7 1 6 2 5 3 7 4

n=8 1 5 2 7 3 6 4 8

然后我总结的规律就是 当你查训的是奇数位置的时候,那个位置的数就是i/2+1.但是当你想查偶数的时候,规律就有点难琢磨透了。至少你不能通过这样的方式来做。

我们以n=5为例

1  2  3  4  5

123456789

我们可以知道在i=2的位置,数字是5 那么我们应该关注一下5是怎么过去的

5         位置  移动了多少具体

              9              0

              8              1

              6              2

              2              4

现在我们不看右边那一列数,只看左边的那一列数,我们可以得到

6-2=n-2/2

8-6=n-6/2

9-8=n-8/2

那么我们就从5的移动过程得到了当你想查询偶数位置上的数是多少的方法。然后当我们知道一开始数所在的位置后,就用奇数的做法就做出来了。

#include
#include
#include
using namespace std;
long long n,m;
int main(){
	int i,j;
	scanf("%I64d%I64d",&n,&m);
	while(m--){
		long long x;
		scanf("%I64d",&x);
		while(x%2==0)
			x+=(n-x/2);
		printf("%I64d\n",x/2+1);
	}
	return 0;
}


总结:找规律不一定是找数值上的变化,位置的变化跟操作过程有关系。

好菜啊!!!!orz.



你可能感兴趣的:(Codeforces,找规律,思考题)