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 */ /*** ...
随机推荐
- JVM基本概念
JVM基础概念 什么是JVM JVM:Java virtual machine,Java虚拟机,它是一种规范.是虚构出来的一台计算机.它可以将二进制字节码根据不同的操作系统转为当前操作系统识别的的字节 ...
- ssh-ssl编译安装
升级前准备 #下载所需依赖包#yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel pam-d ...
- 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通过云盾安骑士-->漏洞管 ...
- sql中数据统计
今天来说一下使用sql统计数据. 用的H2数据库,用的是DBeaver连接工具.有三表,打印表PRINT_JOB,复印表COPY_JOB和扫描表SCANNER_JOB (这段可以忽略)任务是要统计相同 ...
- java小项目
https://blog.csdn.net/redarmy_chen/article/details/11794145#(贪吃蛇) https://blog.csdn.net/likunkun__/a ...
- object 转json 相互转换
1.object 转json 2.json转object 参考 https://blog.csdn.net/justry_deng/article/details/80780175
- MySQL中的约束,添加约束,删除约束,以及其他修饰
一.NOT NULL(非空约束)添加非空约束 1)建表时直接添加 CREATE TABLE t_user(user_id INT(10) NOT NULL); 2)通过ALTER 语句 ALTER T ...
- String--int互转
A:int -->String 1.String s1 = "" + 100; 2.String s2 = String.valueof(100); 3.(int -- In ...
- 正则-Java注释代码
今天写了个匹配java中常见的注释,记录一下,以备后用,使用条件将行两边的空格trim掉. (^\/\*.*)|(^\/\/.*)|(^\*.*)
- 什么是redis的缓存雪崩与缓存穿透?如何解决?
一.缓存雪崩 1.1 什么是缓存雪崩? 首先我们先来回答一下我们为什么要用缓存(Redis): 1.提高性能能:缓存查询是纯内存访问,而硬盘是磁盘访问,因此缓存查询速度比数据库查询速度快 2.提高并发 ...