CodeForces 288C Polo the Penguin and XOR operation

CodeForces - 288C】Polo the Penguin and XOR operation (思维、异或运算) Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p = p0, p1, ..., pn, Polo has defined its beauty — number . Expressi... 阅读详情


题意:给一个数 n,求0~n的一个排列,使得这个排列与0-n的对应异或之和最大。
分析:两个数二进制数正好互补,异或就是最大的,比如,一个数是100,那么我们要找11,(都是二进制)
就是这样找,而且两两正好配对,如果多了一个就是0。怎么找那另一个和它互补的数呢?用11...1 - 本身

借鉴了MZH的代码

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
int dx[]= {0,1,1,1,0,-1,-1,-1};
int dy[]= {1,1,0,-1,-1,-1,0,1};
int gcd (const LL &a, const LL &b) {return b==0?a:gcd (b,a%b);}
using namespace std;
const int maxn=1e6+10;
int a[maxn];
int bitcnt (int n) {
    int cnt=0;
    while (n) {
        n=n>>1; cnt++;
    }
    return cnt;
}
map<LL,LL>pos;
int main() {
    memset (a,0,sizeof (a) );
    int n; scanf ("%d",&n);
    for (int i=n; i>=0; --i) {
        int len=bitcnt (i);//获取位数
        LL maxv= (1<<len)-1;//构造全1的那个二进制数
        if (!pos.count (i) &&!pos.count (maxv^i) ) {
            pos[i]=maxv^i;
            pos[maxv^i]=i;//两两配到一起
        }
    }
    LL sum=0;
    for (int i=0; i<=n; ++i) {sum+=pos[i]^i;}//求和Σi^A[i]
    printf ("%I64d\n",sum);
    for (int i=0; i<=n; ++i) {
        printf ( (i) ?" %I64d":"%I64d",pos[i]);
    }
    return 0;
}


CodeForces 288C - Polo the Penguin and XOR operation 题意:给定一个 n (1 从n ~ 0枚举所有的数,对于每个数找它异或后得到二进制都是 1 的数或者 1 尽量多的数。 对于每个数,先找到与它二进制位数相同的全是 1 的数,然后把这个当成结果,与目前的数异或得到另一个数并记录,然后同时记录另一个数(可能用不到,但是为了节省时间)。 加起来的结果可能超int,故用long long。 #include #include # 阅读详情

相关推荐

CodeForces 288C

C. Polo the Penguin and XOR operation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little penguin Polo l

zjbh89757的专栏 397

CodeForces 288C - Polo the Penguin and XOR operation(思维)

题意: 就是让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为   SUM  =   a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n  构造出一个序列使得和最大 题解: 策略为使得每次异或出来的结果的1尽可能多,而优先从最大的n  开始考虑,因为n  最有可能出更大的数字 代码: #include #inc

q651111937的专栏 1083

CodeForces 288C--异或运算

题意: 给一个数 n ,算出从0到n 的异或运算, ,输出运算后的最大值和p 的排列。 输入: 4 输出: 20 0 2 1 4 3 分析: 要想运算后的结果最大,应该将异或后的值全是1而且是尽可能多的1.因此,先算出最大的数,然后将最大的数与他的位置异 或,如果结果大于 n,将位数减少一位,并将异或后的值和位置记录下来。 代码: #include

shao1996的博客 553

Polo the Penguin and XOR operation 】【CodeForces - 288C】(思维)

题目: Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p = p0, p1, ..., pn, Polo has defined its beauty — number . Exp...

哇哈哈哈的博客 313

CodeForces 288C - Polo the Penguin and XOR operation(思路)

题意:给定一个 n (1 <= n <= 10^6),求(0 ^ p0) + (1 ^ p1) + (2 ^ p2) +…… + (n ^ pn) 的最大值,其中p0 ~ pn为 0 ~ n 中的数,且每个数只利用一次。 从n ~ 0枚举所有的数,对于每个数找它异或后得到二进制都是 1 的数或者 1 尽量多的数。 对于每个数,先找到与它二进制位数相同的全是 1 的数,然后...

dingyaosao9792的博客 151

CodeForces - 288C Polo the Penguin and XOR operation

CodeForces - 288C Polo the Penguin and XOR operation Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p = p0, p1, …, pn...

mandiheyanyu的博客 222

Codeforces Round #177 (Div. 1)C. Polo the Penguin and XOR operation【贪心】

http://codeforces.com/contest/288/problem/C 按二进制位从大到小进行匹配 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #i...

bantengbang8949的博客 182

Polo the Penguin and XOR operation(位运算 思维)

Polo the Penguin and XOR operation CodeForces - 288C Little penguin Polo likes permutations. But most of all he likes permutations of integers from0ton, inclusive. For permutationp = p0, p1, ...

weixin_30825199的博客 146

Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)

题目地址:http://codeforces.com/problemset/problem/288/C 思路:保证位数尽量大的情况下使得每二进制位均为一的情况下结果最大,从后向前枚举(保证位数尽量大),num表示该数i二进制有几位,x即为使得与i异或后每二进制位均为一的数,v[x]标记是否使用过该数,若未使用则与i搭配。

哆啦AC梦的博客 1152

Codeforces Round #177 (Div. 1) 题解【ABCD】

Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符串字典序最小。 题解 先abababab,然后再把k个不同字符输出,那么这样就是最少 代码 #include<bits/stdc++.h> using namespace std; st...

weixin_30914981的博客 96

CodeForces 518A Vitaly and Strings

给出s和t两个字符串,s字典序小于t,问是否可以找到字典序小于t,大于s的字符串,请输出其中一个 本来想了些其他方法,都有问题,最后无奈……… 从s第一个字母开始,构造一个当前位置字母稍大的一个新字符串,后面全部置a,与t进行比较即可得出答案。 #include #include #include #include #include #include #include #include #i

Kim0403的博客 4169

CodeForces 289B Polo the Penguin and Matrix

觉得和矩阵没什么关系,直接看成一个数列,从中位数开始,小的增加d,大的减小d,求操作多少次即可。 如果取余不相等,那肯定不可能转换成同一个数字。 #include #include #include #include #include #include #include #include #include #include #include #include #include #include

Kim0403的博客 1209

CodeForces 289A Polo the Penguin and Segments

 The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds

Kim0403的博客 1055
上一篇: HDU 4594 Script Z
下一篇: POJ 3175 Finding Bovine Roots
Kim0403
博客等级 码龄12年 25粉丝 132原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值