半个月没看cf 手生了很多(手动大哭)

Problem - A - Codeforces

题意

给定数字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;
}

Problem - B1 - Codeforces

题意:对一个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)(博弈论)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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] 题目思路 这些有关位运算的题目肯 ...

  6. 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 ...

  7. 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

    题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...

  8. 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

    题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...

  9. 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation

    题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...

随机推荐

  1. js 中的值类型和引用类型

    javascript中值类型(基本类型):number,string,bool,undefined,null(这5种基本数据类型是按值访问的,因为可以操作保存在变量中的实际的值) 引用类型:对象(Ob ...

  2. Circle Linux镜像在阿里云镜像站首发上线

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 Circle Linux简介 Circle Linux 社区是一个开源.共创的 Linux 社区,将通过完全开放.包容的社区形式与全球开发者共同构建 ...

  3. 虚拟机Centos安装配置

    开始吧~ 新建一个虚拟机 完成后编辑虚拟机 配置内存 处理器: 映像文件: 点击确定完成配置: 开启虚拟机,对操作系统进行配置 输入红线上内容,为计算机选择默认网卡 选择安装时的语言,可选择中文: 设 ...

  4. CVE-2017-11882(Office远程代码执行)

    测试环境:win7+kali+office 2007 xp+kali+office 2003 win7 ip:10.10.1.144 xp ip: 10.10.1.126 kali ip 10.10. ...

  5. linux的一些sao东西

    1.sys命令的目录 /usr/include/asm-generic

  6. linux下串口测试程序

    通过简单的参数配置,执行文件+串口号+波特率 #include <stdio.h> #include <stdlib.h> #include <unistd.h> ...

  7. 写出Hibernate中核心接口/类的名称,并描述他们各自的责任?

    Hibernate的核心接口一共有5个,分别为:Session.SessionFactory.Transaction.Query和 Configuration.这5个核心接口在任何开发中都会用到.通过 ...

  8. 在 Java 中 CycliBarriar 和 CountdownLatch 有什么区别?

    CyclicBarrier 可以重复使用,而 CountdownLatch 不能重复使用. Java 的 concurrent 包里面的 CountDownLatch 其实可以把它看作一个计数器, 只 ...

  9. 什么是RabbitMQ?RabbitMQ的使用场景是什么?

    参考链接:RabbitMQ 简介以及使用场景

  10. 什么是 REST / RESTful 以及它的用途是什么?

    Representational State Transfer(REST)/ RESTful Web 服务是一种帮助计 算机系统通过 Internet 进行通信的架构风格.这使得微服务更容易理解和实现 ...