Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)
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(规律题)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #384 (Div. 2) //复习状压... 罚时爆炸 BOOM _DONE
不想欠题了..... 多打打CF才知道自己智商不足啊... A. Vladik and flights 给你一个01串 相同之间随便飞 没有费用 不同的飞需要费用为 abs i-j 真是题意杀啊, ...
- 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 ...
- 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 ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题
C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...
随机推荐
- LoadRunner免费公开课,惠普金牌讲师亲授
[开课时间]:9月13日 下午2:00—4:00(暂定)[活动费用]:免费[主办单位]:慧都学院[课程形式]:网络在线公开课 LoadRunner简介惠普软件测试解决方案LoadRunner测试实例答 ...
- JokeClient-Swift 仿写学习
required init?(coder aDecoder: NSCoder) 可失败构造器 在init关键字后面添加问号(init?). 可失败构造器会创建一个类型为自身类型的可选类型的对象.你通过 ...
- 实现从Oracle增量同步数据到GreenPlum
简介: GreenPlum是一个基于PostgreSQL数据库开发的MPP架构的数据库仓库,适用于OLAP系统,支持50PB(1PB=1000TB)级海量数据的存储和处理. 背景: 目前有一个业务是需 ...
- mssqlserver数据导出到另外一个数据库
mssqlserver数据导出到另外一个数据库 准备源数据库,找到想要导出的数据库,右键选择"任务"再选择"导出数据" 设置源数据库信息 3.设置目标数据库,导 ...
- 按照TYPE的文件导入导出功能
/** * 导入文件Action;*/private File excelFile;// 保存原始文件名private String excelFileFileName;// 保存原始文件名priva ...
- 树莓派3B初始化后一些必须的设置
接上一篇,SSH已经登录成功(http://www.cnblogs.com/crosys/p/6220108.html) 1.树莓派系统的设置 1.1扩展系统空间 因为内存卡还有很多空间没有分配,第一 ...
- MySql.Data.Entity 在EF中解析uint的枚举时有BUG
当枚举继承uint类型时无法获取值.
- SqlServer SET IDENTITY_INSERT ON | OFF
想要将值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 示例: 1.首先建立一个有标识列的表: )) 2.尝试在表中做以下操作: , 'gar ...
- 重叠div鼠标经过事件
两个div重叠了,但是下面的div有鼠标移入移出事件,发现当鼠标移入或者移出时事件会执行两次,尝试了在上层div阻止事件,判断div所在位置……,后来发现只要一个css属性即可解决该问题,在上层div ...
- [游戏开发-学习笔记]菜鸟慢慢飞(九)- NGUI- UIPanel(官方说明翻译)
我自己笔记是做在OneNote上,直接复制粘贴过来变成图片了,效果好像还可以. 机器翻译,我自己看了一下,改了一部分.