【47.40%】【codeforces 743B】Chloe and the sequence

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Chloe, the same as Vladik, is a competitive programmer. She didn’t have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad.

Let’s consider the following algorithm of generating a sequence of integers. Initially we have a sequence consisting of a single element equal to 1. Then we perform (n - 1) steps. On each step we take the sequence we’ve got on the previous step, append it to the end of itself and insert in the middle the minimum positive integer we haven’t used before. For example, we get the sequence [1, 2, 1] after the first step, the sequence [1, 2, 1, 3, 1, 2, 1] after the second step.

The task is to find the value of the element with index k (the elements are numbered from 1) in the obtained sequence, i. e. after (n - 1) steps.

Please help Chloe to solve the problem!

Input
The only line contains two integers n and k (1 ≤ n ≤ 50, 1 ≤ k ≤ 2n - 1).

Output
Print single integer — the integer at the k-th position in the obtained sequence.

Examples
input
3 2
output
2
input
4 8
output
4
Note
In the first sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1]. The number on the second position is 2.

In the second sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1]. The number on the eighth position is 4.

【题目链接】:http://codeforces.com/contest/743/problem/B

【题解】

如果k==2^(n-1)则直接输出.
否则递归搞一下就可以了。(因为处理的都是相同的问题);
如果在左边k就变成k-2^(n-1);否则还是k.
n的话固定-1;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
LL k;
LL pre[51];

void solve(int n,LL k)
{
    if (k==pre[n-1])
        cout << n << endl;
    else
        if (k < pre[n-1])
            solve(n-1,k);
        else
            solve(n-1,k-pre[n-1]);
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    pre[0] = 1;
    rep1(i,1,50)
        pre[i] = pre[i-1]*2;
    rei(n);rel(k);
    solve(n,k);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626819.html