Codeforces Round #384(Div. 2)B. Chloe and the sequence【思维+lowbit】
来源:互联网 发布:约爱cms源码程序.zip 编辑:程序博客网 时间:2024/06/05 10:57
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!
The only line contains two integers n andk (1 ≤ n ≤ 50,1 ≤ k ≤ 2n - 1).
Print single integer — the integer at the k-th position in the obtained sequence.
3 2
2
4 8
4
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); }}
- Codeforces Round #384(Div. 2)B. Chloe and the sequence【思维+lowbit】
- Codeforces Round #384 (Div. 2) B Chloe and the sequence
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence 找规律
- Codeforces Round #384 (Div. 2) 743B Chloe and the sequence
- Codeforces Round #384 B. Chloe and the sequence
- Codeforces 743B Chloe and the sequence
- 743B. Chloe and the sequence codeforces
- Codeforces 743B-Chloe and the sequence
- ***CodeForces - 743B Chloe and the sequence
- Codeforces-743B-Chloe and the sequence(二进制思维题)
- Chloe and the sequence CodeForces
- Chloe and the sequence CodeForces
- B. Chloe and the sequence
- 【47.40%】【codeforces 743B】Chloe and the sequence
- codeforces 743 B Chloe and the sequence(递归)
- codeforces 743 B. Chloe and the sequence (规律题)
- Codeforces Round #371 (Div. 2) B-Filya and Homework【思维】
- 欢迎使用CSDN-markdown编辑器
- githup上Android APP好用的文本工具
- 高速公路ETC卡签之我见7-用户卡发行
- 点击下拉框其他地方下拉框收起
- java中判断文件是否为空内容
- Codeforces Round #384(Div. 2)B. Chloe and the sequence【思维+lowbit】
- 配置JAVA环境变量
- ubuntu查看硬件信息
- jsp页面 隐藏和显示内容
- android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法
- 笔记-js将long日期格式转换为标准日期格式
- 百度地图之基本功能
- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 更改linux用户登录shell的操作方法