codeforces round # 384 div2 B Chloe and the sequence 神奇二进制找规律题

链接:https://codeforces.com/contest/743/problem/B

题意:n-1次操作,每次往数列后加未出现过的最小数字后,再将原数列复制一次粘到新序列末尾后,问第n-1次操作后 第k位是几。 

题解:看这个序列 [1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1]. 

1出现坐标对应的二进制为[1,11,101,111,1001,1011,1111] 

2,,,,,,,,,,,[10,110,1010,1110] 

3,,,,,,,,,,,[100,1100] 

4,,,,,,,,,,,[1000] 

二进制找规律题:从后往前1第一次出现的位置即为当前位置上的值;然后把k化成二进制,找第一个1出现的位置

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;

int main()
{
    int n;
    long long k;
    cin>>n>>k;
    int a[maxn];
    int cnt=0;
    while(k)
    {
        a[cnt++]=k%2;
        k/=2;
    }
    int ans=0;
    for(int i=0; i<cnt; i++)
    {
        ans++;
        if(a[i]==1)
            break;
    }
    cout<<ans;
}

猜你喜欢

转载自www.cnblogs.com/sweetlittlebaby/p/12680019.html
今日推荐