Codeforces Round #384 B. Chloe and the sequence

来源:互联网 发布:linux chmod nobody 编辑:程序博客网 时间:2024/06/05 09:31
Chloe and the sequence
原题链接:http://codeforces.com/problemset/problem/743/B

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.


题目大意:

假设现在的数列是a,那么在a的背后放一个最小的没出现过的整数,然后再把a重复的放在后面。

然后现在问你k位置是什么

(注意坑点,,用long  long)

较为零乱的代码:(找规律)
#include<stdio.h>#define ll long long intll a[55];int main(){    a[0]=0;    a[1]=1;    ll i,cnt,n,k;    for(i=2; i<53; i++)        a[i]=2*a[i-1]+1;    scanf("%lld%lld",&n,&k);    if(k&1)//奇数为1        printf("1\n");    else    {        int flag=1;        while(flag)        {            ll ans;            for(i=52; i>=0; i--)                if(a[i]<k)                {                    cnt=k-a[i]-1;//规律在此,当cnt为0时,当前的i+1就是所需要的答案了                    if(cnt==0)                    {                        ans=i+1;                        flag=0;                        break;                    }                    else                        k=cnt;                }            printf("%lld\n",ans);        }    }    return 0;}

大神的理解:实际上就是这个数字k的二进制最小的1的位数的位置。
代码:
#include<bits/stdc++.h>using namespace std;long long n,p;int main(){    scanf("%lld%lld",&n,&p);    cout<<log2(p&(-p))+1<<endl;}
                        最重要的还是思想啊。啊奋斗
1 0
原创粉丝点击