Uvalive 3675 Sorted bit sequence (digit dp+ binary) __uvalive

Source: Internet
Author: User
Topic link

Uvalive 3675 Sorted bit sequence

All integers in the interval [l,r] [l,r] are sorted from small to large according to the number 1 in the binary representation. If the number of 1 is the same, the number is sorted by size. Find the number of K K in this sequence. Where negative numbers are represented by the complement: the binary of a negative number means that the sum of the binary of its opposite numbers is exactly equal to 232 2^{32}.
Data size: Lxr≥0,−231≤l≤r≤231−1,1≤k≤min (r−l+1,2147473547) lxr≥0, -2^{31}≤l≤r≤2^{31}-1, 1≤k≤min (R−L + 1, 2 147 4 73 547). Analysis

Refer to the analysis method in the paper.
First notice a condition l∗r≥0 L*r\geq 0.
We first consider the case where M and N m and n are positive numbers.
Since the first keyword of the sort is 1, the second keyword is the size of the number, so it is easy to determine the number of 1 in the answer: in the statistical interval [M,n] The number of binary representations containing 1 is 0,1,2,... Number, until the cumulative answer exceeds K K, the current value is the number of the answer containing 1, assuming that s S. This digital DP can be resolved by enumerating the number of 1 that need to be counted, Dfs.
At the same time, we also find that the answer is a few [m,n] containing S 1 of the number. Therefore, only a two-point answer , to find [L,ans] [L,ans] containing the number of S S 1 to judge it.

In the case of l<0,r<0 L, I'm going to turn the interval into positive intervals, and according to a binary of negative numbers, the binary sum of the opposite number is exactly equal to 232 2^{32}, and the result will be changed back.

Special handling of l=0 or r=0 l=0 or r=0 is required.

Because of the l∗r≥0 L*r\geq 0, the actual most troublesome discussion: The l<0,r>0 L0 situation has been avoided. Code

Https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=245&page= show_problem&problem=1676 #include <stdio.h> #include <string.h> #include <algorithm> #include &
Lt;math.h> using namespace std;
typedef long Long LL;

CONST LL base = 1ll << 32;
int T, digit[35];
ll L, R, K, LL, RR;

ll dp[35][35][35], cnt[4][35];
    ll DFS (int pos, int pre, int limit, int sum) {if (pos = 1) return pre = = sum;
    if (Pre > Sum) return 0;
    if (!limit && dp[pos][pre][sum]!=-1) return dp[pos][pre][sum]; int last = limit?
    Digit[pos]: 1;
    ll ret = 0;
    for (int i = 0; I <= last; ++i) {ret = DFS (pos-1, pre + I, limit && (i = n), sum);
    } if (!limit) dp[pos][pre][sum] = ret;
return ret;
    ll solve (ll x, int id, int flag) {memset (digit, 0, sizeof (digit));
int len = 0;
    printf ("x =%lld\n", x);
        while (x) {digit[len++] = x% 2;
 x/= 2;   } if (flag!=-1) {return DFS (len-1, 0, 1, flag);
    } memset (Cnt[id], 0, sizeof (cnt[id));
        for (int i = 1; I <= ++i) {//Don't forget 32. Cnt[id][i] = DFS (len-1, 0, 1, i);
    Enum has I 1//printf ("cnt[%d][%d] =%lld\n", id, I, cnt[id][i]);
return 0;
    } void work (int flag) {solve (LL, 0,-1);
    Solve (RR, 1,-1);
    ll prefix = 0;
    int goal;
        for (int i = 1; I <= ++i) {//Don't forget 32.
        Prefix + + (Cnt[1][i]-cnt[0][i]);
            if (prefix >= K) {prefix = (Cnt[1][i]-cnt[0][i]);
            Goal = i;
        Break
    }//printf ("Goal =%d\n", goal);
    ll left = K-prefix + Cnt[0][goal];
    ll high = RR, low = ll, mid;
        While [low < high] {mid = (1LL * low + high)/2;
        ll tmp = Solve (Mid,-1, goal);
        if (TMP < left) is low = mid + 1;
    else High = mid;
    ll ans = high;
    if (flag) ans =-(Base-high);
printf ("%lld\n", ans); } int MAin () {memset (DP,-1, sizeof (DP));
    scanf ("%d", &t); while (t--) {//Note n * M >= 0 scanf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.