题目链接:A Leapfrog in the Array

题意:给出1~n的n个数,从小到大每隔一个位置放一个数。现在从大到小把数往前移动,每次把最右边的数移动最靠右边的空格处直到n个数都在前n个位置。

Codeforces 950D  A Leapfrog in the Array (思维)-LMLPHP

题解:从数据的大小就可以看出这题一定是推公式的题,那么假设现在一个数刚刚移动到了x位置,那么这个数之前一定有x/2个数没有移动过,所有这个数后面就有n-x/2个数(这里包括x本身)。所以x在移动之前的位置就是x+(n-x/2)。所以一直将所要求的x 按 x =  x+(n-x/2)这个公式一直向上求直到x为奇数为止就是最初的位置。

 #include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5+;
const int INF = 1e9+;
int N,M,T,S;
long long n,q;
int main(){
while(cin>>n>>q){
long long t = *n-;
long long p = ;
while(t > p){
t -= p;
p *= ;
}
//cout<<"!!!!!"<<endl;
for(int i=;i<q;i++){
long long x;
scanf("%lld",&x);
if((x+)% == ){
cout<<(x+)/<<endl;
}
else{
while(!(x&)){
x += n - x/;
}
cout<<x/+<<endl;
}
}
}
return ;
}
04-26 14:38
查看更多