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 ...
随机推荐
- BigDecimal求余操作
BigDecimal求余操作如下: package com.qiu.lin.he; import java.math.BigDecimal; public class CeShi { public s ...
- mydumper 找不到libmysqlclient.so.20
报错信息: mydumper: error while loading shared libraries: libmysqlclient.so.20: cannot open shared objec ...
- HDU2546题解
解题思路:先对价格排序(顺序或倒序都可以),然后,对前n-1(从1开始.排序方式为顺序)做容量为m(卡上余额)-5的01背包(背包体积和价值相等).假设dp[i][j]表示从前i个背包中挑选体积不超过 ...
- verilog 之数字电路 寄存器,触发器。
我一直听说没有由code到circuit就只是入门了.实在没办法了.我想了一招,一个一个的写,然后看RTL,然后分析.这是第一篇. 1.触发器. 没有复位,置位.posedge clk 是触发沿时钟. ...
- Apache中按天分割日志(Windows)
网上很多资料都有对Apache的access.log按天生成的方法,但在Windows server下稍有不同: 1.打开httpd.conf配置文件找到: CustomLog "logs/ ...
- 为什么KVM计算机点无故重启?
一.故障1:机器hangs 本地一台cloudstack计算节点无故连不上了,cloudstack也坏了,后查看有一台系统虚拟机在这台计算节点上,导致cs挂了.去找到这台机器后,发现这台机器卡住了,重 ...
- 深入浅出 Java Concurrency (14): 锁机制 part 9 读写锁 (ReentrantReadWriteLock) (2)
这一节主要是谈谈读写锁的实现. 上一节中提到,ReadWriteLock看起来有两个锁:readLock/writeLock.如果真的是两个锁的话,它们之间又是如何相互影响的呢? 事实上在Reen ...
- 配置git 环境变量
1.从Git官网下载windows版本的git:http://git-scm.com/downloads 2.一般使用默认设置即可:一路next,git安装完毕! 3.但是如果这时你打开windows ...
- GameObject.DestroyImmediate(go, true)会使磁盘资源数据丢失,导致不可用
GameObject.DestroyImmediate(go, true)会使磁盘资源数据丢失,导致不可用 第二个参数true表示 allowDestroyingAssets,表示允许销毁资源. 实测 ...
- bootstrap 的页码显示问题-------------德州
之前一个小bug,无论上边怎么搜索,下边的页码,不会改变 调整: 1,在mapper中添加计数, 2,找到service,queryPage中添加, 3,关键一部,如果没有会报错:,找不到该列 so, ...