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

Copy
4 3
2
3
4
Output
3
2
4
Input

Copy
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].

题意 : 题意看这四张图就可以了,还是很明确的,每次将最末位的元素移动到前面的一个空位置上。

思路分析 : 暴力写一下,发现它的移动是有规律的,奇数位的数是不发生变动的

代码示例 :

#define ll long long
const int maxn = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ll n, q, x;
cin >> n >> q; while(q--){
cin >> x;
if (x % 2) {
cout << x/2+1 << endl;
}
else {
ll t = n - x/2;
while(t % 2 == 0){
x += t;
t /= 2;
}
cout << (x+t)/2+1 << endl;
}
}
return 0;
}

CF - 一直交换元素的规律的更多相关文章

  1. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

  2. “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...

  3. jquery插件——点击交换元素位置(带动画效果)

    一.需求的诞生 在我们的网页或者web应用中,想要对列表中的元素进行位置调整(或者说排序)是一个常见的需求.实现方式大概就以下两种,一种是带有类似“上移”.“下移”的按钮,点击可与相邻元素交换位置,另 ...

  4. iOS Swift 数组 交换元素的两种方法

    swap(&arr[fromIndexPath.row], &arr[to.row]) (arr[fromIndexPath.row],arr[to.row]) = (arr[to.r ...

  5. js 实现数组元素交换位置

    /** * 数组元素交换位置 * @param {array} arr 数组 * @param {number} index1 添加项目的位置 * @param {number} index2 删除项 ...

  6. 【codeforces】【比赛题解】#849 CF Round #431 (Div.2)

    cf的比赛越来越有难度了……至少我做起来是这样. 先看看题目吧:点我. 这次比赛是北京时间21:35开始的,算是比较良心. [A]奇数与结束 "奇数从哪里开始,又在哪里结束?梦想从何处起航, ...

  7. 如何交换两个等长整形数组使其数组和的差最小(C和java实现)

    1. 问题描述: 有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使[数组a元素的和]与[数组b元素的和]之间的差最小. 2. 求解思路: 当前数组a和数组 ...

  8. jQuery使用之(一)标记元素属性

    jQuery使用主要介绍jQuery如何控制页面,包含元素的属性.css样式风格.DOM模型.表单元素和事件处理等. 标记元素的属性 html中每一个标记都具有一些属性,他们这个标记在页面中呈现各种状 ...

  9. 查询无序列表中第K小元素

    当需要在无需列表中寻找第k小的元素时,一个显然的方法是将所有数据进行排序,然后检索k个元素.这种方法的运行时间为O(n log(n)). 无序列表调用分区函数将自身分解成两个子表,其长度为i和n-i. ...

随机推荐

  1. element-ui—dialog使用过程中的坑

    场景一:我们将dialog写成一个可复用的公共组件用于显示不同内容(如表格操作中的修改或添加的弹窗),之后发现dialog的遮罩将弹出层(点击修改或添加后理应由一个弹窗显示出来)都盖住了,而我想要的效 ...

  2. UVa 11134 - Fabled Rooks——[问题分解、贪心法]

    We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...

  3. 关于对height:100%的研究

    参考此链接: https://segmentfault.com/a/1190000012707337

  4. vue权限篇

    前言 在一个项目中,一些功能会涉及到重要的数据管理,为了确保数据的安全,我们会在项目中加入权限来限制每个用户的操作.作为前端,我们要做的是配合后端给到的权限数据,做页面上的各种各样的限制. 需求 因为 ...

  5. 2019-8-31-dotnet-删除只读文件

    title author date CreateTime categories dotnet 删除只读文件 lindexi 2019-08-31 16:55:58 +0800 2019-02-28 1 ...

  6. 转:EBS-自动获取/创建CCID

    DECLARE l_ccid NUMBER; l_msg ); l_chart_of_account_id NUMBER; l_set_of_book_id NUMBER; BEGIN l_set_o ...

  7. mac如何查看已连接wifi的密码

    可以通道mac自带的“钥匙串访问”功能查看.选择需要查询的wifi名称,右击选择“将密码拷贝到剪贴板”,输入管理员密码后,密码就拷贝好了. 找个地方粘贴即可看到密码

  8. mysql主从之binlog的工作模式

    一 三种模式介绍 1.1 查看mysql主库的binlog格式 binlog仅在主库设置即可,从库无需设置 binlog的默认方式为STATEMENT ( show variables like '% ...

  9. 使用poi读写excel、向excel追加数据等,包括.xls和.xlsx文档

    1.使用maven引入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>p ...

  10. 对sql server查询速度的优化

    处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考 ...