题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大。

析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100,那么我们只要找11,(都是二进制)

这不就正好么,一试,果然是这样。就是这样找,而且两两正好配对,如果多了一个就是0呗,也就是0.那知道一个数,怎么找那另一个和它互补的呢?其实很简单,

就是用111-100=11,就是利用这个,就能很轻松把这个题解决,注意可能超int,要用long long。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
int ans[maxn]; int main(){
int n;
cin >> n;
memset(ans, -1, sizeof(ans));
LL sum = 0;
for(int i = n; i >= 0; --i){
if(ans[i] > -1) continue;
for(int j = 0; ; ++j){
if((1 << j)-1 >= i){
int t = (1<<j) - 1;
ans[i] = t - i;
ans[t-i] = i;
sum += ((1<<j)-1) << 1;
break;
}
}
}
cout << sum << endl;
for(int i = 0; i <= n; ++i)
if(!i) printf("%d", ans[i]);
else printf(" %d", ans[i]);
printf("\n");
return 0;
}

CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)的更多相关文章

  1. Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)

    time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard inputoutput: standa ...

  2. codeforces B. Polo the Penguin and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...

  3. Codeforces 288E - Polo the Penguin and Lucky Numbers(数位 dp+推式子)

    题目传送门 似乎我的解法和官方题解不太一样 纪念自己独立做出来的一道难度 2800 的题. 我们记 \(ans(x)\) 为 \([444...44,x]\) 的答案,显然答案为 \(ans(r)-a ...

  4. CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)

    题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求: 1.如果从1-k房间开始走,一定能直到 ...

  5. CodeForces 288A Polo the Penguin and Strings (水题)

    题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...

  6. CodeForces 289B Polo the Penguin and Matrix (数学,中位数)

    题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...

  7. CodeForces 289A Polo the Penguin and Segments (水题)

    题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...

  8. ZOJ 4057 XOR Clique(位运算)

    XOR Clique BaoBao has a sequence a​1​,a​2,...,a​n. He would like to find a subset S of {1,2,...,n} s ...

  9. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

随机推荐

  1. (转)Tomcat 启动后 “闪退”

    缘由 今天在一台新机器上部署开发环境,安装完Tomcat以后,运行startup.bat后出现“闪退”...在网上找到了解决方案,条理清晰且分析的很详尽.记录如下: 首先贴出原文链接: http:// ...

  2. python学习笔记(十三): 多线程多进程

    一.线程&进程 对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程, ...

  3. linux tcp调优

    Linux TCP Performance Tuning News Linux Performance Tuning Recommended Books Recommended Links Linux ...

  4. Nodejs&Express

    http://www.expressjs.comhttp://github.com/lelandtseng/form-datahttp://github.com/lelandtseng/mongo-m ...

  5. webpy简单使用

    #!/usr/bin/env python import web import pymysql.cursors # Connect to the database connection = pymys ...

  6. js调用activeX插件 报异常:TypeError:对象不支持 属性方法

    部署之后的js网页如果调用没有签名的 ocx/dll 插件的话会报异常:TypeError:对象不支持 “init” 属性方法 (init为插件公开的方法) 但是如果写一个htm本地文件去调用插件,和 ...

  7. PHP - declare tick

    今天看php手册,结构流程控制语句,翻到个没见过的关键字declare(申明的意思).于是稍稍看了下. 大概意思是:定义一段代码.关于他合适执行,怎么执行完全依照他的参数. 反正不知道什么意思.先来一 ...

  8. sublime +react+es6开发环境

    Babel Sublime3才有的插件,支持ES6.JSX语法高亮. 菜单->View->Syntax->Open all with current extension as...- ...

  9. 网络编程基础之粘包现象与UDP协议

    一.粘包现象原理分析 1.我们先来看几行代码,从现象来分析: 测试程序分为两部分,分别是服务端和客户端 服务端.py #!/usr/bin/env python3 #-*- coding:utf-8 ...

  10. screen command of linux

    [screen command of linux] 常用键:   补充: Ctrl-a S  # split terminal horizon Ctrl-a TAB   # switch to ano ...