CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 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 (位运算,异或)的更多相关文章
- Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)
time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard inputoutput: standa ...
- codeforces B. Polo the Penguin and Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...
- Codeforces 288E - Polo the Penguin and Lucky Numbers(数位 dp+推式子)
题目传送门 似乎我的解法和官方题解不太一样 纪念自己独立做出来的一道难度 2800 的题. 我们记 \(ans(x)\) 为 \([444...44,x]\) 的答案,显然答案为 \(ans(r)-a ...
- CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)
题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求: 1.如果从1-k房间开始走,一定能直到 ...
- CodeForces 288A Polo the Penguin and Strings (水题)
题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...
- CodeForces 289B Polo the Penguin and Matrix (数学,中位数)
题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...
- CodeForces 289A Polo the Penguin and Segments (水题)
题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...
- ZOJ 4057 XOR Clique(位运算)
XOR Clique BaoBao has a sequence a1,a2,...,an. He would like to find a subset S of {1,2,...,n} s ...
- Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算
D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...
随机推荐
- node.js 获取客户端信息
结果:
- 为什么KVM计算机点无故重启?
一.故障1:机器hangs 本地一台cloudstack计算节点无故连不上了,cloudstack也坏了,后查看有一台系统虚拟机在这台计算节点上,导致cs挂了.去找到这台机器后,发现这台机器卡住了,重 ...
- 第一个 Windows 界面程序
编译器 使用的编译器为 Visual Studio 2017 菜单栏 -> 文件 -> 新建 -> 项目 选择 Windows 桌面应用程序,然后填好相关信息后点击“确定” 在解决方 ...
- IDA Pro 权威指南学习笔记(十三) - 基本代码转换
IDA提供的代码转换包括: 1.将数据转换为代码 2.将代码转换为数据 3.指定一个指令序列为函数 4.更改现有函数的起始或结束地址 5.更改指令操作数的显示格式 代码显示选项 通过 Options ...
- windows server 2008 配置DNS服务器与IIS
0x00: 总结这个星期在学校学的. 0x01安装: 首先你得安装好windows server 2008 然后在添加角色->安装IIS和DNS服务器 勾选好你要安装的. 安装-> 根据老 ...
- svn ignore 的用法(忽略文件及目录)
svn ignore 的用法(忽略文件及目录) 若想创建了一个文件夹,并且把它加入版本控制,但忽略文件夹中的所有文件的内容: $ svn mkdir spool $ svn propset svn:i ...
- [Z]shell变量详解
原文:http://www.cnblogs.com/barrychiao/archive/2012/10/22/2733210.html 1 shell变量基础shell变量是一种很“弱”的变量,默认 ...
- 基于vue-easytable实现数据的增删改查
基于vue-easytable实现数据的增删改查 原理:利用vue的数据绑定和vue-easetable的ui完成增删改查 后端接口: 1.条件查询表中数据 http://localhost:4795 ...
- SpringMVC配置过程中出现的问题!
<c:set var="ctx" value="${pageContext.request.contextPath}" />不起作用,原因是web. ...
- 安装sql server 2000
昨天下午快下班的时候 因为公司需要折腾了下sql server 2000,先不说这么古老的版本,而且安装的也是醉了... 首先sql server 2000是基于32位的系统开发的,那时候据说还没有6 ...