Codeforces Round #469 (Div. 2) D. A Leapfrog in the Array (思维)

write picture description here

The main idea of ​​the title : The number i in the first n numbers is at position 2*i-1, and then the column is filled with numbers from right to left. The last q queries ask you what number should be filled in for Xi in pos.

Analysis : First of all, the range of n is 1e18. Direct simulation is definitely not possible, but there must be some rules in it. When we write and see by ourselves, when n=6, the positions of 1 to 6 are 1 4 2 6 3 5 respectively, and when n=3, 1 The positions of ~3 are 1 3 2 respectively. Here we can clearly find that when Xi&1, the last number of this position is Xi/2+1.

Proving the above small inference is also very simple, because initially there are 2*n positions, and there are actually only n numbers, so the numbers in the odd-numbered positions in the leftmost n positions are actually impossible to move.

Then we consider other positions. The premise of one jump of a number (at position pos) is that there are any number of consecutive padding blocks on its adjacent left side, and
all the padding blocks on the left side have pos/2 numbers at the beginning. To his left, there are n-pos/2-1 numbers left on his right initially, so we can deduce that if we push back and set the position in the query to be p, then the position after each jump is p+ (np/2-1)+1, when (p+(np/2))&1 indicates that he has reached the original position, at this time (New Xi)/2+1 is the answer

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
#include<string.h>
using namespace std;
typedef long long ll;

#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define pi acos(-1.0)
#define lson  root << 1
#define rson  root << 1 | 1

ll n,q;

ll a[200005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>q;
    for(int i=0;i<q;i++)
    {
        ll x;
        cin>>x;
        while(1)
        {
            if(x%2==1)
                break;
            x=x+(n-x/2);
        }
        cout<<x/2+1<<endl;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325714207&siteId=291194637