Codeforces 460 D. Little Victor and Set

暴力+构造

If r?-?l?≤?4 we can all subsets of size not greater than k.
Else, if k?=?1, obviously that answer is l. If k?=?2,
answer is 1, because xor of numbers 2x and 2x?+?1 equls 1.
If k?≥?4 answer is 0 because xor of
to pairs with xor 1 is 0.

If k?=?3, we can choose numbers 2x and 2x?+?1 with xor 1.
So we need to know, if we can get xor equals 0. Suppose that
there are 3 such numbers xy and z (r?≥?x?>?y?>?z?≥?l)
with xor equals 0. Consider the most non-zero bit of numberx.
At the same bit of y it‘s also 1, because xor equls
0, and y?>?z. Consider the next bit of numbers. If z have 0 there,
we have to do next: set the previous bit of numbers x and y equals 0,
and set current bit equals 1. Obviously xor still equals
0, z hadn‘t changed and numbers x and y stood
closer to z, so they are still at [l,?r].And x?>?y.Consider
the next bit of numbers. If z has zero here than we will change most bits of x и y at
the same way and so on. z?>?0, so somewhen we will get to bit in which z has 1.
Since xorequals 0, the same bit of x would
be 1 because x?>?y, and y would
have 0 there. At the next bits we will change bit in x to 0,
and in numbers y and z to 1.Finally z would
be greater or equal than before, and x would be less or greater than before, and x?>?y?>?z would
be correct. So, we have the next: if such numbers xy and z exist
than also exist numbers:

1100…000

1011…111

0111…111

with xor equals 0. There are not much such triples, so we
can check them.

D. Little Victor and Set

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that
has the following properties:

  • for all x  the
    following inequality holds l?≤?x?≤?r;
  • 1?≤?|S|?≤?k;
  • lets denote the i-th element of the set S as si;
    value  must
    be as small as possible.

Help Victor find the described set.

Input

The first line contains three space-separated integers l,?r,?k (1?≤?l?≤?r?≤?1012; 1?≤?k?≤?min(106,?r?-?l?+?1)).

Output

Print the minimum possible value of f(S). Then print the cardinality of set |S|.
Then print the elements of the set in any order.

If there are multiple optimal sets, you can print any of them.

Sample test(s)

input

8 15 3

output

1
2
10 11

input

8 30 7

output

0
5
14 9 28 11 16

Note

Operation  represents
the operation of bitwise exclusive OR. In other words, it is the XOR operation.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

LL L,R,K;
LL ans=0x3f3f3f3f3f3f3f3f;

int main()
{
    cin>>L>>R>>K;
    if(R-L+1<=4)
    {
        LL m=R-L+1;
        LL sig=0;
        for(LL i=1;i<(1LL<<m);i++)
        {
            LL temp=0;
            LL wei=0;
            LL si=i;
            while(si)
            {
                wei++;
                si=si&(si-1);
            }
            if(wei>K) continue;
            for(LL j=0;j<m;j++)
            {
                if(i&(1LL<<j))
                {
                    temp^=L+j;
                }
            }
            if(temp<ans)
            {
                ans=temp;
                sig=i;
            }
        }
        cout<<ans<<endl;
        LL wei=0;
        LL tsig=sig;
        while(tsig)
        {
            wei++;
            tsig=tsig&(tsig-1);
        }
        cout<<wei<<endl;
        for(LL i=0;i<m;i++)
        {
            if(sig&(1<<i))
            {
                cout<<L+i<<" ";
            }
        }
        cout<<endl;
    }
    else
    {
        if(K==1)
        {
            cout<<L<<endl;
            cout<<1<<endl;
            cout<<L<<endl;
        }
        else if(K==2)
        {
            if(L%2) L++;
            cout<<1<<endl;
            cout<<2<<endl;
            cout<<L<<" "<<L+1<<endl;
        }
        else if(K==3)
        {
            bool flag=false;

            LL mx=3,mi=1;
            while(mx<=R)
            {
                if(mi>=L)
                {
                    flag=true;
                    cout<<0<<endl<<3<<endl;
                    cout<<mx<<" "<<mx-1<<" "<<mi<<endl;
                    break;
                }

                mx<<=1LL;
                mi<<=1LL; mi++;
            }

            if(flag==false)
            {
                if(L%2) L++;
                cout<<1<<endl;
                cout<<2<<endl;
                cout<<L<<" "<<L+1<<endl;
            }
        }
        else
        {
            cout<<0<<endl;
            cout<<4<<endl;
            if(L%2) L++;
            cout<<L<<" "<<L+1<<" "<<L+2<<" "<<L+3<<endl;
        }
    }
    return 0;
}
时间: 2024-10-28 10:04:31

Codeforces 460 D. Little Victor and Set的相关文章

Codeforces 460 D Little Victor and Set (构造)

题目链接 构造的好题.表示是看了题解才会做的. 假如[l,r]长度不超过4,直接暴力就行了. 假如[l,r]长度大于等于5,那么如果k = 1,显然答案应该是l:如果k=2,可以找到a^(a+1)=1:如果k=3,首先只取两个就得到一个下界为1,但是可能出现为0的情况,下面再仔细讨论.如果k>=4,可以找到两组a^(a + 1) = 1,所以答案是0. 现在剩下的问题就是如何判断k=3的情况答案能否为0了.答案为0时我们只有可能取了3个数,设它们为x,y,z,并且不妨设有l <= z <

codeforces 460D:Little Victor and Set

Description Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties: for all x the following in

Codeforces 460D Little Victor and Set(构造)

题目链接:Codeforces 460D Little Victor and Set 题目大意:给定范围l,r,选小于k个数,使得这些数的亦或和最小. 解题思路:加入k为偶数,那么kXOR(k+1)=1 根据这个可以处理掉k≠3的所有情况. 对于k=3的情况,找到一个大于l的2k,如果满足2k+1≤r,那么就可以构造出三个数亦或值为0的情况. #include <cstdio> #include <cstring> #include <algorithm> using

Codeforces 460D Little Victor and Set(看题解)

Little Victor and Set 其他都很好求, 只有k == 3的时候很难受.. 我们找到第一个不大于l的 t, 答案为 l, 3 * t, (3 * t) ^ l 感觉好像是对的, 感觉又不会证明, 啊, 我好菜啊. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #

codeforces 460D Little Victor and Set(构造、枚举)

最近的CF几乎都没打,感觉挺水的一个题,不过自己仿佛状态不在,看题解才知道做法. 输入l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)). 从[l,r]选至多k个数使得选出的数的异或值最小,输出最小异或值和方案. 分类讨论,首先如果r-l+1<=4,枚举集合解决之. 先面讨论r-l+1>=5的情况: 此时有至少5个数可以选择,故至少有连续的4个数满足2x,2x+1,2x+2,2x+3. k==1时显然方案为{l}.k==2时,显然方案

Codeforces 460D Little Victor and Set --分类讨论+构造

题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四个数(L,L+1,L+2,L+3),这样的话(L^(L+1)) ^ ((L+2)^(L+3)) = 0,最优 如果L不是偶数,那么看从L+1到R有没有四个数,如果有则取该四个数,否则最小异或和达不到0,也达不到1了,不再考虑k=4,k=3时还有可能等于0,所以转到k=3 2. k=3时,要使异或和为0,那

Codeforces Round #460 (Div. 2) 919A. Supermarket 919B. Perfect Number 919C. Seat Arrangements

这场cf有点意思,hack场,C题等于1的特判hack很多人(我hack成功3个人,上分了,哈哈哈,咳咳...) D题好像是树形dp,E题好像是中国剩余定理,F题好像还是dp,具体的不清楚,最近dp的题目好多,一会滚去学dp. 写A,B,C的题解. A. Supermarket time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Codeforces Round #460 (Div. 2)_D. Substring_[dp][拓扑排序]

题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的value等与这条路径上出现次数最多的字母的数目,求该图的最大value 比赛时,用dfs超时,看官方题解用的dp和拓扑排序,a--z用0-25表示,用dp[i][j]表示以第i个结点结尾的路径上第j个字母出现的次数 拓扑排序每排到一个点,就用该点的dp去更新与它相邻点的dp,最开始入度为0的点特殊处理了一下,dp过程中同步更新结果res 也复习了一下拓扑排序 #include<iostream> #include<cstdio&

Codeforces Round #460 (Div. 2)

A 签到 B 题意 定义:一个数(没有前缀0)的各个位数之和为10位"perfec"数,问第k个"perfect"数位多少(1<=k<=1e5) 分析 一开始找错了,以为会超过1e9,通过理性的分析不难发现,最大不超过1e9,强行打个表即可 C 签到 D 题意 n个点m条边的有向图,每个点有一个数字(可以重复,0~25),定义一条路径的权值为该路径出现数字最多的数字的次数,若有环输出-1,否则输出最大值 分析 思路:首先直接dfs肯定不行,最坏情况n^2