Codeforces 460D Little Victor and Set(构造)

248 篇文章 0 订阅
33 篇文章 0 订阅
博客详细解析了Codeforces 460D题目Little Victor and Set的解决方案。当给定范围l, r和整数k时,如何选择少于k个数使得它们的亦或和最小。文章指出,当k为偶数时,可以通过特定规律构造解,而对于k=3的特殊情况,通过寻找大于l的2k且满足2k+1≤r的条件,可以构造出亦或和为0的三个数。" 117167252,10544914,物理层详解:C语言差分双相码编码与通信基础,"['计算机网络', '物理层', '数据通信', '编码技术', '信号处理']
摘要由CSDN通过智能技术生成

题目链接:Codeforces 460D Little Victor and Set

题目大意:给定范围l,r,选小于k个数,使得这些数的亦或和最小。

解题思路:加入k为偶数,那么kXOR(k+1)=1
根据这个可以处理掉k≠3的所有情况。
对于k=3的情况,找到一个大于l的2k,如果满足2k+1r,那么就可以构造出三个数亦或值为0的情况。

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

using namespace std;
typedef long long ll;

ll l, r, k;

void solve (int n) {
    int s = 1;
    ll ret = l;

    for (int i = 2; i < (1<<n); i++) {
        ll u = 0;
        for (int j = 0; j < n; j++)
            if (i&(1<<j))
                u ^= (l+j);

        if (u < ret) {
            ret = u;
            s = i;
        }

    }

    int x = 0;
    ll arr[10];
    for (int i = 0; i < n; i++)
        if (s&(1<<i))
            arr[x++] = l + i;

    printf("%lld\n%d\n", ret, x);
    printf("%lld", arr[0]);
    for (int i = 1; i < x; i++)
        printf(" %lld", arr[i]);
    printf("\n");
}

void fuck () {
    ll x = 1;
    while (x <= l) x <<= 1;

    if ((x | (x>>1)) <= r) {
        printf("0\n3\n");
        printf("%lld %lld %lld\n", l, x | (x>>1), (x | (x>>1)) ^ l);
        return;
    }

    printf("1\n2\n");
    if (l&1)
        l++;
    printf("%lld %lld\n", l, l+1);
}

int main () {
    scanf("%lld%lld%lld", &l, &r, &k);

    if (k == 1)
        printf("%lld\n1\n%lld\n", l, l);
    else if (k >= 5) {

        if (l&1)
            l++;
        printf("0\n4\n%lld %lld %lld %lld\n", l, l+1, l+2, l+3);
    } else if (k == 4 && (r - l + 1 > 4 || l%2 == 0)) {

        if (l&1)
            l++;
        printf("0\n4\n%lld %lld %lld %lld\n", l, l+1, l+2, l+3);
    } else if (k == 2 && (r - l + 1 > 2 || l%2 == 0)) {

        if (l&1)
            l++;
        printf("1\n2\n%lld %lld\n", l, l+1);
    } else if (k == 2) {

        if ((l^r) > l)
            printf("%lld\n1\n%lld\n", l, l);
        else
            printf("%lld\n2\n%lld %lld\n", l^r, l, r);
    } else if (k == 4) {
        solve(k);
    } else {
        fuck();
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值