Codeforces 949B A Leapfrog in the Array(找规律)

题目链接:A Leapfrog in the Array

题意

最初有一个长度为 2n1 2 n − 1 的数组,数组的每个偶数位都是空的,数组的 2x1 2 x − 1 位上的数字为 x x ,然后将这个数组的最后一个数字放到数组的最后一个空着的位上,不断执行这个操作,直到数组中不再有空位为止,最终数组的长度为 n,对于一个长度为 2n1=7 2 n − 1 = 7 的的初始数组,操作如下:

对于一个长度为 n n 的最终数组,q 次询问它的第 x x 位上的数字的值。

输入

第一行为两个整数n,q (1n1018,1q2×105),接下去 q q 行每行一个整数 x

输出

对于每次询问,输出最终数组 x x 位上的数字。

样例

输入
4 3
2
3
4
输出
3
2
4
提示
最终数组如上图所示。
输入
13 4
10
5
4
8
输出
13
3
8
9
提示
最终数组为:[1,122,8,3,11,4,9,5,13,6,10,7]

题解

对于每次询问,若 x x 为奇数,则答案为 x+12,否则将 x x 位上的数字还原到原来的位置上,第 x 位上的数字,假设它左边所有的奇数位的数字都已经归位,偶数位上的数字都已经到它的后面,现在到这个位置上的数字还原,那么它左边的数字有 x2 x 2 个,右边的数字有 nx2 n − x 2 ,因此这个数字一次还原的位置为 x+nx2 x + n − x 2 ,不断还原下去,直到 x x <script type="math/tex" id="MathJax-Element-21">x</script> 为奇数为止。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
LL n, q;
LL x, Index;

int main() {
    #ifdef LOCAL
        freopen("test.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%I64d%I64d", &n, &q) != EOF) {
        while(q--) {
            scanf("%I64d", &x);
            while(x % 2 == 0) {
                x += n - x / 2;
            }
            printf("%I64d\n", (x + 1) / 2);
        }
    }

    return 0;
}
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值