Codeforces Round #384(Div. 2)B. Chloe and the sequence【思维+lowbit】

来源:互联网 发布:约爱cms源码程序.zip 编辑:程序博客网 时间:2024/06/05 10:57

B. Chloe and the sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 to1. 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 andk (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 is2.

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 is4.


题目大意:

初始序列只有一个数字1,接下来我们需要进行(n-1)次操作,接下来我们每一次都需要在序列尾部加上一个没出现过的最小正整数.再接下来将之前的序列复制一次放在序列末尾,让我们判断进行了n-1次操作之后,第k个位子上的数字是多少。


思路:


1、每次增加的序列长度肯定是和2的倍数相关的,那么我们肯定要把思路矛头指向二进制上来。


2、那么我们分析这样一个序列:

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1.

其中,我们先分析1 2 3 4,其所在位子为1,2,4,8.都是2的整数次方,对应我们将位子转化成二进制:

0

10

100

1000

很显然,对于这个位子上的值,就是逆序来看,第一个1出现的位子。

那么接下来我们对于这个观察得到的结果类推到其他数字以及其位子上来看,其结果是正确的。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int a[60];#define ll __int64int main(){    ll n,k;    while(~scanf("%I64d%I64d",&n,&k))    {        int cnt=0;        while(k)        {            a[cnt++]=k%2;            k/=2;        }        /*        for(int i=0;i<cnt;i++)        {            printf("%d",a[i]);        }        printf("\n");*/        int output=0;        for(int i=0;i<cnt;i++)        {            output++;            if(a[i]==1)            {                break;            }        }        printf("%d\n",output);    }}






0 0
原创粉丝点击