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 */ /*** ...
随机推荐
- ssh中pam模块造成认证失败
由于编译参数增加了pam认证 安装后发现这个模块没启用 vim /etc/ssh/sshd_config UsePAM yes 将这个模块打开后就ok了
- 7月3日 Django 头像预览、用户上传文件操作、logging、debug_tool_bar
1. 注册功能 1. 头像预览 //头像预览 $('#id_avatar').change(function () { console.log(this.files[0]) //找到选中的头像文件 v ...
- 记录java中常用的英文单词01
专业缩写 POJO(plain ordinary java object)--简单的java对象 Spring-jdbc--为了使JDBC更加易于使用,spring在JDBC API上定义了一个抽象层 ...
- AVRmega16 LED 例程
AVRmega16 LED 例程 ...
- Ubuntu一键安装LAMP,LNMP
参考: https://blog.csdn.net/xueyingqi/article/details/50674078 https://www.cnblogs.com/pengzheng/p/363 ...
- cookies、sessionStorage和localStorage的区别
cookies.sessionStorage和localStorage的区别 对比 特性 Cookie LocalStorage SessionStorage 数据的生命周期 ...
- python3 爬虫4--解析链接
1.urlparse() 属于urllib.parse 在urlparse世界里面,一个标准的URL链接格式如下 scheme://nrtlooc/path;paramters?query#fragm ...
- PWA全称Progressive Web App,即渐进式WEB应用?
一个 PWA 应用首先是一个网页, 可以通过 Web 技术编写出一个网页应用. 随后添加上 App Manifest 和 Service Worker 来实现 PWA 的安装和离线等功能解决了哪些问题 ...
- kafka producer如何优化打入速度?
增加线程 提高 batch.size 增加更多 producer 实例 增加 partition 数 设置 acks=-1 时,如果延迟增大:可以增大 num.replica.fetchers(fol ...
- 什么是Hystrix断路器?我们需要它吗?
由于某些原因,employee-consumer公开服务会引发异常.情况下使用Hystrix我们定义了回退方法.如果在公开服务中发生异常,则回退方法返回一些默认值 . 如果firstPage metho ...