CodeForces - 949B

##CodeForces - 949B

题意:现在给你一个n,表示有2*n-1个方格,第奇数方格上会有一个数字 1-n按顺序放。第偶数个方格上是没有数字的。变动规则是排在最后一个位置的数字,移动到它前边最近的空位 。 直到数字之间没有空位。最终的数列是由n已经确定的。给你q,表示q次查询,每次查询输入一个x,问数列第x位上的数字是多少? .img

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

当n=13时,数列为[1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].

题意:

中文的我想大家都看得懂

分析:

假设现在询问的位置是 x , 且 ( 1 &lt; = x &lt; = n ) x,且(1&lt;=x&lt;=n) x,1<=x<=n

奇数位置的值一定能立马确定为 ( x + 1 ) / 2 (x+1)/2 (x+1)/2,因为奇数位置刚开始都有值且确定,往前移动只能移动到偶数位置上

  • 如果 x x x是奇数

    v a l = ( x + 1 ) / 2 ​ val=(x+1)/2​ val=(x+1)/2

  • x x x是偶数

    假 设 最 后 x 数 的 位 置 的 数 为 v a l , 那 么 我 们 回 到 v a l 刚 放 到 x 这 个 位 置 的 状 态 , 此 时 位 置 x 之 前 的 偶 数 位 置 一 定 是 空 的 假设最后x数的位置的数为val,那么我们回到val刚放到x这个位置的状态,此时位置x之前的偶数位置一定是空的 xvalvalxx, 即 位 置 x 前 面 有 x / 2 个 数 , 又 因 为 v a l 刚 放 到 x 这 个 位 置 , 故 x 后 面 的 数 一 定 是 连 续 的 数 共 有 n − x / 2 − 1 个 即位置x前面有x/2个数,又因为val刚放到x这个位置,故x后面的数一定是连续的数共有n-x/2-1个 xx/2valxxnx/21,

    那 么 v a l 放 到 位 置 x 之 前 一 定 在 位 置 x + ( n − x / 2 − 1 ) + 1 的 位 置 上 那么val放到位置x之前一定在位置x+(n-x/2-1)+1的位置上 valxx+(nx/21)+1 如 果 此 时 x 是 奇 数 , 则 v a l = ( x + 1 ) / 2 , 否 则 重 复 x 为 偶 数 的 状 态 如果此时x是奇数,则val=(x+1)/2,否则重复x为偶数的状态 xval=(x+1)/2,x 直 到 x 为 奇 数 即 可 ​ 直到x为奇数即可​ x

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e3+100;
const int branch=26;
const int inf=0x7fffffff;
int main()
{
    ll n,q,x;
    ios::sync_with_stdio(false);/*这语句加速cin输入,去了也没关系*/
    cin>>n>>q;
    while(q--)
    {
        cin>>x;
        while(x%2==0)
        {
            x=n+x/2;
        }
        cout<<(x+1)/2<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值