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:

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<stdio.h>
#include<string.h>
#include<algorithm>
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.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值