Codeforces 460d little Victor and set (construction, enumeration)

Source: Internet
Author: User

Almost none of the recent CF attacks, and I feel quite confused. However, I seem to be in a bad state. I can only know how to solve the problem.

InputL, Bytes,R, Bytes,K(1 digit ≤ DigitLLimit ≤ limitRLimit ≤ limit 1012; 1 ≤ bytesKLimit ≤ limitMin(106. Explain,RAccept-Encoding-LRows + rows 1 )).

Selecting at most K numbers from [L, R] minimizes the exclusive or value of the selected number, and outputs the smallest exclusive OR value and the scheme.

 

Classification: first, if R-l + 1 <= 4, the enumeration set is solved.

First, we will discuss the situation of R-l + 1> = 5:

At this time, you can select at least five numbers. Therefore, at least four consecutive numbers meet the requirements of 2x, 2x + 1, 2x + 2, and 2x + 3.

When K = 1, it is clear that the solution is {L }. When K = 2, it is clear that the solution is {2x, 2x + 1 }. K> = 4, obviously the solution is {2x, 2x + 1, 2x + 2, 2x + 3 }.

When K = 3, consider the following:

First, the exclusive or value is 1 (refer to k = 2)

We are looking for whether the difference or value can be 0. If yes, it is obvious that three numbers are selected. Set x> Y> Z.

111... 1111

111... 1110

000... 0001

Obviously, the first half of X, Y, and Z must be like this, but since we want to make X, Y, and Z as close as possible, the first half of X, Y, and Z must be as follows:

11

10

01

After that, each time you add a digit, it may be Yi = Zi = 1, xi = 0 or xi = Zi = 1, YI = 0 or xi = YI = 1, Zi = 0.

Because X, Y, and Z are as close as possible, YI = Zi = 1, Zi = 0 is obviously adopted.

Therefore, the binary format of X, Y, and Z is as follows:

110... 0

101... 1

011... 1

At this point, the problem is roughly solved. The remaining issues are some details, but the problem is not serious.

 

#include <cstdio>#include <cstring>#include <iostream>#include <vector>using namespace std;#define ll long longint cnt(int i){    int ret=0;    while(i) i-=i&(-i), ++ret;    return ret;}int main(){    ll l,r;    int k;    while(~scanf("%I64d%I64d%d",&l,&r,&k)){        if(r-l+1<5){            int n=r-l+1;            ll ansxor=1ll<<60;            vector<ll>val;            for(int i=1;i<(1<<n);++i){                ll xx=0;                for(int j=0;j<n;++j)                    if(i&(1<<j)) xx^=l+j;                if(xx<ansxor && cnt(i)<=k){                    ansxor=xx;                    val.clear();                    for(int j=0;j<n;++j) if(i&(1<<j)) val.push_back(l+j);                }            }            printf("%I64d\n",ansxor);            printf("%d\n",val.size());            for(int i=0;i<val.size();++i) printf("%I64d%c",val[i],i==val.size()-1?‘\n‘:‘ ‘);        }        else if(r-l+1>=5){            if(k==1){printf("%I64d\n1\n%I64d\n",l,l);continue;}            if(k==2){                if(l&1) l++;                puts("1");                puts("2");                printf("%I64d %I64d\n",l,l+1);            }            else if(k>=4){                if(l&1) l++;                puts("0");                puts("4");                printf("%I64d %I64d %I64d %I64d\n",l,l+1,l+2,l+3);            }            else if(k==3){                ll x=-1,y,z;                for(ll i=3;i<=r;i=i<<1){                    if((i^(i-1))>=l){                        x=i;                        y=i - 1;                        z=i^(i-1);                        break;                    }                }                if(x!=-1){                    puts("0");                    puts("3");                    printf("%I64d %I64d %I64d\n",x,y,z);                }                else {                    if(l&1) l++;                    puts("1");                    puts("2");                    printf("%I64d %I64d\n",l,l+1);                }            }        }    }    return 0;}

 

Codeforces 460d little Victor and set (construction, enumeration)

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.