B. A Leapfrog in the Array(思维,逆推)

. . . . . . . . . 遇到这种规律题就脑子疼.........

r x 假设一个数从r位置换到x位置

r , ①、 r位置是最后一个数,否则不会被换

x x / 2 , x n x / 2 1 ②、x位置左边有x/2个数字,x右边有n-x/2-1个数

x n x / 2 1 ③、x右边的n-x/2-1个数是连在一起的

x , x + ( n x / 2 1 + 1 ) 所以已知当前位置是x,上一个位置就是x+(n-x/2-1+1)

x + ( n x / 2 ) , x 退 有就是x+(n-x/2),一直循环直到x是奇数退出循环

? 为什么?到了奇数说明已经到了自己的位置

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,q;
int main()
{
	cin >> n >> q;
	for(int i=1;i<=q;i++)
	{
		ll x;
		cin>>x;
		while(1)
		{
			if(x%2==1)	break;
			x=x+(n-x/2);
		}
		cout<<(x-1)/2+1<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/106844635