Codeforces Round #721 (Div. 2)A. And Then There Were K(位运算,二进制) B1. Palindrome Game (easy version)(博弈论)
半个月没看cf 手生了很多(手动大哭)
题意
给定数字n, 求出最大数字k, 使得 n & (n−1) & (n−2) & (n−3) & ... (k) = 0
题解
&:有0则0
假如n转为二进制共有x位 , 要使&后=0, k的最高位需=0
我们使最高位=0,后面都为1; 那么此数+1=什么
=100000(x-1个0), 这样就能&后=0了
-------------------------------------------------------------------------
结论
k= (1<<x - 1)
1左移x位再-1
代码
#include <iostream> using namespace std; int main()
{
int n, t;
cin >> t;
while(t --)
{
cin >> n;
int s = 0;
while(n)
{
s ++;
n >>= 1;
}
s --;
cout << (1 << s) - 1 << endl;
} return 0;
}
题意:对一个01回文串,每次有两个操作(1)将一个0变为1,消耗1美元;(2)将字符串翻转,不消耗金钱(条件: 1). 此时不是回文串 2). 上次别人操作没用翻转)当串全为1时游戏结束,花钱少的人胜,问谁胜。
题解:先手初始时为回文, 不能翻转,但只要他改变了一个数,就破坏了回文串的属性,使得后手可以翻转;因此显然大多数情况下后手能让先手多花两美元,从而后手胜。
但也有一些特殊情况,比如一开始串就全为1,那么直接平局;
假如回文串是奇数长度且最中间为0,那么先手还可以操作一下:如果此时只有这中间的一个0,那自然还是输;但假如不是,那么先手把中间的0转换了就先后手易位,两 极 反 转,此时虽然先手多花了1美元,但根据上面的结论,局势转换后后手方可以让先手多花两美元,因此可必胜。
附: 先手一定会少花2美元原因
假如该串为0000
操作: A:0100 --> B:1100 --> A: 1110 -->B: 翻转成0111 --> A: 1111
A花费3元, B花费1元
代码
#include <iostream>
#include <cstring> using namespace std; int main()
{
int n, t;
cin >> t;
while(t --)
{
int n, sum = 0;
string s; bool f1 = 0;
cin >> n;
cin >> s;
int n1 = s.length();
if(n1 % 2 && s[n/2] == '0')
s[n/2] = '1', f1 = 1; for(int i = 0; i < n1/2; ++ i)
if(s[i] == '0')
sum ++; if(f1 && sum > 0)
cout << "ALICE" << endl;
else if(f1 || !f1 && sum > 0)
cout << "BOB" << endl;
else
cout << "DRAW" << endl;
} return 0;
}
部分参考于 Codeforces Round #721 (Div.2)部分题解 - 知乎 (zhihu.com)
Codeforces Round #721 (Div. 2)A. And Then There Were K(位运算,二进制) B1. Palindrome Game (easy version)(博弈论)的更多相关文章
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- Codeforces Round #267 (Div. 2) B. Fedor and New Game【位运算/给你m+1个数让你判断所给数的二进制形式与第m+1个数不相同的位数是不是小于等于k,是的话就累计起来】
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...
- Codeforces Round #672 (Div. 2) B. Rock and Lever题解(思维+位运算)
题目链接 题目大意 给你一个长为n(n<=1e5)的数组,让你求有多少对a[i]和a[j] (i!=j)满足a[i]&a[j]>a[i]^a[j] 题目思路 这些有关位运算的题目肯 ...
- Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】
B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...
随机推荐
- Numpy库基础___二
ndarray一个强大的N维数组对象Array •ndarray的变换 x.reshape(shape)重塑数组的shape,要求元素的个数一致,不改变原数组 x = np.ones((2,3,4), ...
- Linux内核升级修复系统漏洞-RHSA-2017:2930-Important: kernel security and bug fix update
公司使用的阿里云服务器(Centos7.4 x86_64bit)内核版本为:3.10.0-693.21.1.el7.x86_64, 2019年3月4日 02:07:58通过云盾安骑士-->漏洞管 ...
- 嵌入式无操作系统下管理内存和队列(类UCOS II思想)
例子:存储日志,最多存128条,每条最大1MB. 内存方面 因为嵌入式不适合用动态内存,会产生碎片.这里我们用 u8 data[LOG_SIZE];开辟固定128MB的内存区,再对其分为128个1MB ...
- nginx+keepalived 高可用方案
nginx+keepalived 高可用方案 准备工作 192.168.157.11 192.168.157.12 安装nginx 跟新yum源文件 rpm -ivh http://nginx.org ...
- /proc/uptime参数的意义
有关/proc/uptime这个文件里两个参数所代表的意义: [root@app ~]#cat /proc/uptime 3387048.81 3310821.00 第一个参数是代表从系统启动到现在的 ...
- Nextcloud fpm 版在 Dokcer 下安装踩坑
安装 首先到 https://registry.hub.docker.com/_/nextcloud 获取 Nextcloud 的示例 docker-compose version: '2' volu ...
- [vmware to openstack] 安装virtio驱动
描述 从vmware的虚拟机迁移到opensatck,由于vmware虚拟机磁盘类型采用的是scsi,要迁移到opensatck平台运行,发现虚拟机无法起来,原因是vmware虚拟机没有安装vir ...
- Vue基于webpack自动装载配置
Vue的自动装载配置是在 @cli/cli-service 包中,配置文件的目录在 lib/config/ 下的文件,css.js 文件是配置样式的处理,先从这里开始了解把 CSS配置流程 对应着这 ...
- read,readline,readlines的特点与区别
1.read 读取全部文件 with open("test.text", "r",encoding='utf8') as f: print(f.read()) ...
- maven项目改造成springboot项目
springboot项目其实归根到底就是一个maven项目,通常我们创建springboot项目,只要使用idea中的spring Initializr就可以创建就可以了. 今天我们来讲下如何改造一个 ...