Codeforces Round #608 (Div. 2) E. Common Number

链接:

https://codeforces.com/contest/1271/problem/E

题意:

At first, let's define function f(x) as follows:
f(x)={x2x−1if x is evenotherwise
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1)=[1], path(15)=[15,14,7,6,3,2,1], path(32)=[32,16,8,4,2,1].

Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?

Formally speaking, you need to find maximum y such that |{x | 1≤x≤n,y∈path(x)}|≥k.

思路:

假设当前值为x,是奇数,则可以从x2, x2+1,推出,接着是x4,x4+1,x4+2,x4+3...
如果是偶数,要多计算x+1。
然后可以二分枚举,但是没搞懂为什么要分奇偶数。

代码:

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

LL n, k;

LL Check(LL x)
{
    LL cnt = 1LL, tmp = 1LL;
    while(x*2+tmp*2-1<= n)
    {
        x *= 2;
        tmp *= 2;
        cnt += tmp;
    }
    cnt += max(0LL, n-x*2+1);
    return cnt;
}

int main()
{
    while(cin >> n >> k)
    {
    LL l = 0, r = (n-1)/2+1;
    while(r-l>1)
    {
        LL mid = (l+r)/2;
        if (Check(2*mid+1) >= k)
            l = mid;
        else
            r = mid;
    }
    LL ans = 2*l+1;
    l = 0, r = n/2+1;
    while(r-l>1)
    {
        LL mid = (l+r)/2;
        if (Check(2*mid)+Check(mid*2+1) >= k)
            l = mid;
        else
            r = mid;
    }
    cout << max(ans, 2*l) << endl;
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/12089078.html