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 ...
随机推荐
- OC字符串基本操作
不可变的字符串的修改方法有返回值(重新指向新的字符串地址) 可变的字符串的修改方法没有返回值(修改字符串本身) // NSString 不可变字符串 // 1.创建字符串对象 // 使用初始化方法创建 ...
- Javascript不同浏览器差异及兼容方法
原文链接:http://caibaojian.com/js-ie-different-from-firefox.html javascript的各种兼容就是为了解决不同浏览器的差异性,了解其中的差异能 ...
- 多值(in),范围值(between..and)
多值检测 关键词[in] 查出年龄是23,24,28 的人员信息 select * from T_Employee where FAge in (23,25,28) in 后面如果跟子查询, ...
- 用Retrofit发送请求中添加身份验证
用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...
- Linux下ps命令详解 Linux下ps命令的详细使用方法
http://www.jb51.net/LINUXjishu/56578.html Linux下的ps命令比较常用 Linux下ps命令详解Linux上进程有5种状态:1. 运行(正在运行或在运行队列 ...
- yii2 随笔
view 1.添a标签 use yii\helpers\Html; //需要引用html组件 <?= Html::a('点击', ['sign/sign','id' => '01']) ? ...
- 关于 AVI 的一些代码
#ifndef __HSS_AUTO_REVISE_AVI_FRAMERATE_HSS__ #define __HSS_AUTO_REVISE_AVI_FRAMERATE_HSS__ /******* ...
- 教程三:Wechat库的使用
上一篇教程中我们提供了wechat的php的库,这里我们简要介绍一个这个库的源码和使用.这个库的主文件为`Wechat.php`,其余的几个文件都是为这个文件服务的,提供加解密,消息拼接等功能.`We ...
- Laravel与Repository Pattern(仓库模式)
为什么要学习Repository Pattern(仓库模式) Repository 模式主要思想是建立一个数据操作代理层,把controller里的数据操作剥离出来,这样做有几个好处: 把数据处理逻辑 ...
- [LeetCode] Nth Highest Salary 第N高薪水
Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...