题目描述:
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.
输入描述:
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.
输出描述:
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima’s algorithm finishes.
输入:
4 3
2
3
4
13 4
10
5
4
8
输出:
3
2
4
13
3
8
9
题意:
现在给你一个n,表示有2*n-1个方格,第奇数方格上会有一个数字 1-n按顺序放。第偶数个方格上是没有数字的。变动规则是排在最后一个位置的数字,移动到它前边最近的空位 。 直到数字之间没有空位。最终的数列是由n已经确定的。给你q,表示q次查询,每次查询输入一个x,问数列第x位上的数字是多少? .
题解:
找规律
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main(){
ll n,q;
while(scanf("%lld%lld",&n,&q)!=EOF){
while(q--){
ll x;
scanf("%lld",&x);
while(x % 2 == 0){
x = n + x / 2;
}
printf("%lld\n",(x + 1) / 2);
}
return 0;
}
return 0;
}