传送门

Description

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.

Sample Input

3 2

4 8

Sample Output

2

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.

思路

题意:

有一个序列,由1 - n 的数字组成,第一个元素是 1 ,接下来把前一步所得到的序列加在后面并且在这两个序列中间插上n个数中未使用过的最小数,问第k个数是什么。

题解:

将序列写出来可以发现规律,1 + 2x 的位置值都是 1,2 + 4x 的位置的值都是 2,4 + 8x 的位置的数都是 3,8 + 16x 的位置的数都是 4……,因此按照这个规律就可以知道第k个数是谁了。

#include<bits/stdc++.h>
using namespace std;
typedef __int64 LL;

LL pow(LL x,LL n)
{
	LL res = 1;
	while (n)
	{
		if (n & 1)
		{
			res = res*x;
		}
		x *= x;
		n >>= 1;
	}
	return res;
}

int main()
{
	LL n,k;
	scanf("%I64d%I64d",&n,&k);
	for (int i = 0;;i++)
	{
		LL tmp = pow(2,i);
		if ((k - tmp) % (tmp*2) == 0)
		{
			printf("%d\n",i+1);
			break;
		}
	}
	return 0;
}

递归求解  

#include<bits/stdc++.h>
using namespace std;
typedef __int64 ll;
int  work(ll n,ll k)
{
    ll p=pow(2,n-1);
    if(k>p) work(n-1,k-p);
    else if(k<p) work(n-1,k);
    else return n;
}

int main()
{
    ll n,k;
    cin>>n>>k;
    cout<<work(n,k)<<endl;
    return 0;
}

  

Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)的更多相关文章

  1. Codeforces Round #384 (Div. 2)B. Chloe and the sequence 数学

    B. Chloe and the sequence 题目链接 http://codeforces.com/contest/743/problem/B 题面 Chloe, the same as Vla ...

  2. Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp

    D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...

  3. Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)

    传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...

  4. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  5. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  6. Codeforces Round #384 (Div. 2) //复习状压... 罚时爆炸 BOOM _DONE

    不想欠题了..... 多打打CF才知道自己智商不足啊... A. Vladik and flights 给你一个01串  相同之间随便飞 没有费用 不同的飞需要费用为  abs i-j 真是题意杀啊, ...

  7. Codeforces Round #384 (Div. 2)A,B,C,D

    A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #384 (Div. 2) A B C D dfs序+求两个不相交区间 最大权值和

    A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题

    C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...

随机推荐

  1. 纯CSS3实现多层云彩变换飞行动画

    查看效果:http://hovertree.com/texiao/css3/4/效果2 效果图: 代码如下: <!doctype html> <html lang="zh& ...

  2. iOS:GCD组

    组内异步会与组外顺序执行的事件争抢资源 1).创建一个组 dispatch_group_t group = dispatch_group_create(); 2).组内异步ST1,DISPATCH_Q ...

  3. FlashBuilder4安装SVN插件步骤

    1. 选择菜单 帮助–> 安装新软件 2. 在使用里键入地址:  http://subclipse.tigris.org/update_1.6.x并点击添加 在Subclipse栏里选择带有Re ...

  4. android基于口令加密快速搞懂(一)

    import java.util.Random; import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypt ...

  5. Oracle读取excel

    --解析excel,转换成table,可供查询,支持xls.xlsx --首先修改这个Type,长度改为4000. CREATE OR REPLACE TYPE XYG_PUB_DATA_UPLOAD ...

  6. 使用专业的消息队列产品rabbitmq之centos7环境安装

      我们在项目开发的时候都不可避免的会有异步化的问题,比较好的解决方案就是使用消息队列,可供选择的队列产品也有很多,比如轻量级的redis, 当然还有重量级的专业产品rabbitmq,rabbitmq ...

  7. phpStudy2016 配置多个域名期间遇到的问题

    第一步 在C:\Windows\System32\drivers\etc下的hosts文件下添加   第二步   找到Apache 下的httpd.conf  文件 打开,去掉171行前边的#   第 ...

  8. 修复 Windows7 资源管理器左侧收藏夹无法展开问题

    相信大家在网上搜多到的解决办法大多数都是修改注册表,但是这个办法多数是无效的 1.运行regedit 2.展开到HKEY_CLASSES_ROOT\lnkfile 3.添加一个字符串值:IsShort ...

  9. MMORPG大型游戏设计与开发(服务器 AI 事件)

    AI中的事件与场景中的事件大致相同,都是由特定的条件触发的.只不过AI的事件与其他事件不同的是,对于AI的事件往往是根据不同的AI类型,和动态的触发条件下才产生的.其实不管AI多么智能,它对应的触发条 ...

  10. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...