CF949B A Leapfrog in the Array

思路:

最终的时候,对于位置p,若p是奇数,则该位置的元素是(p + 1) / 2;若p是偶数,需要从p开始不断地迭代寻找上一次跳跃所处的位置(p = p + n - p / 2),直到p是奇数为止。这个过程直观上看是log(n)的,因为每次跳跃的长度都在n / 2级别。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int main()
 5 {
 6     ll n, q, x;
 7     cin >> n >> q;
 8     while (q--)
 9     {
10         cin >> x;
11         while (!(x & 1)) x += n - x / 2;
12         cout << (x + 1 >> 1) << endl;
13     }
14     return 0;
15 }
原文地址:https://www.cnblogs.com/wangyiming/p/8638773.html