1. 程式人生 > 實用技巧 >CodeForces 950D-A Leapfrog in the Array(打表找規律)

CodeForces 950D-A Leapfrog in the Array(打表找規律)

題目連結:https://codeforces.com/problemset/problem/950/D
CSDN食用連結:https://blog.csdn.net/qq_43906000/article/details/107865886

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 ≤ 10^{18}, 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 \(x _i (1 ≤ x_ i ≤ 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].

題目大意:給你一個序列,從1到n,每個數之間剛開始有個間隙,現在每次將最後一個元素填入最後的空缺中,最後得到一個完整的前n個數的序列,現在問你對於n個這樣操作後第x個位置的值是什麼,詢問有q個。

emmm,如果位置是奇數的話位置就是確定的,即\((pos+1)/2\),如果是偶數呢?emmm,感覺不太會。。。沒關係,我們可以打個表來找找規律,對於前20個數其1到n的排列如下:

1: 1
2: 1 2
3: 1 3 2
4: 1 3 2 4
5: 1 5 2 4 3
6: 1 4 2 6 3 5
7: 1 6 2 5 3 7 4
8: 1 5 2 7 3 6 4 8
9: 1 9 2 6 3 8 4 7 5
10: 1 6 2 10 3 7 4 9 5 8
11: 1 9 2 7 3 11 4 8 5 10 6
12: 1 7 2 10 3 8 4 12 5 9 6 11
13: 1 12 2 8 3 11 4 9 5 13 6 10 7
14: 1 8 2 13 3 9 4 12 5 10 6 14 7 11
15: 1 12 2 9 3 14 4 10 5 13 6 11 7 15 8
16: 1 9 2 13 3 10 4 15 5 11 6 14 7 12 8 16
17: 1 17 2 10 3 14 4 11 5 16 6 12 7 15 8 13 9
18: 1 10 2 18 3 11 4 15 5 12 6 17 7 13 8 16 9 14
19: 1 15 2 11 3 19 4 12 5 16 6 13 7 18 8 14 9 17 10
20: 1 11 2 16 3 12 4 20 5 13 6 17 7 14 8 19 9 15 10 18

emmm,似乎發現了什麼不得了的東西。也就是說每一行可以由上一行推出來的。假設求的是第13行的第10位,那麼他是由第12行的第8位+1得來的,而第12行的第八位是由第11行的第6位+1得來\(\leftarrow (10,4)+1\leftarrow(9,2)+1\leftarrow(8,0)+1=(8,8)+1\leftarrow(7,6)+1\leftarrow(6,4)+1\leftarrow(5,2)+1\leftarrow(4,4)+1\leftarrow(3,2)+1\leftarrow(2,2)+1\leftarrow(1,1)+1\),一旦位置被確定位奇數了就可以返回了,所以就可以這麼往上推了。

其模擬片段如下:

ll ans=0,n1=n;
while (!(x&1)) {
	ll m=x/2;
	x=n1-m;
	n1-=m;
	ans+=m;
}
printf("%lld\n",ans+((x+1)>>1LL));

以下是AC程式碼:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mac=2e5+10;

int main(int argc, char const *argv[])
{
	ll n,q;
	scanf ("%lld%lld",&n,&q);
	while (q--){
		ll x;
		scanf ("%lld",&x);
		if (x&1) printf("%lld\n",(x+1)>>1LL);
		else {
			ll ans=0,n1=n;
			while (!(x&1)){
				ll m=x/2;
				x=n1-m; n1-=m;
				ans+=m;
			}
			printf("%lld\n",ans+((x+1)>>1LL));
		}
	}
	return 0;
}