Atcoder 2373 Cookie Exchanges
Problem Statement
Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:
- Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.
This action will be repeated until there is a person with odd number of cookies in hand.
How many times will they repeat this action? Note that the answer may not be finite.
Constraints
- 1≤A,B,C≤109
Input
Input is given from Standard Input in the following format:
A B C
Output
Print the number of times the action will be performed by the three people, if this number is finite. If it is infinite, print -1 instead.
Sample Input 1
4 12 20
Sample Output 1
3
Initially, Takahashi, Aoki and Snuke have 4, 12 and 20 cookies. Then,
- After the first action, they have 16, 12 and 8.
- After the second action, they have 10, 12 and 14.
- After the third action, they have 13, 12 and 11.
Now, Takahashi and Snuke have odd number of cookies, and therefore the answer is 3.
Sample Input 2
14 14 14
Sample Output 2
-1
Sample Input 3
454 414 444
Sample Output 3
1
a==b==c的时候会死循环,其他情况暴力算就行了,因为每一次操作之后最大值-最小值会减半,所以不久就能到达终止条件。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int A,B,C,tot,a,b,c;
int main(){
scanf("%d%d%d",&A,&B,&C);
while(!((A&1)||(B&1)||(C&1))){
if(A==B&&B==C){
puts("-1");
return 0;
} tot++,a=(B+C)>>1,b=(A+C)>>1,c=(B+A)>>1;
A=a,B=b,C=c;
} printf("%d\n",tot);
return 0;
}
Atcoder 2373 Cookie Exchanges的更多相关文章
- Atcoder Grand-014 Writeup
A - Cookie Exchanges 题面 Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respec ...
- AtCoder Grand Contest 014
AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...
- 【AtCoder】AGC014
AGC014 链接 A - Cookie Exchanges 发现两个数之间的差会逐渐缩小,所以只要不是三个数都相同,那么log次左右一定会得到答案 #include <bits/stdc++. ...
- AtCoder Grand Contest 014 题解
A - Cookie Exchanges 模拟 Problem Statement Takahashi, Aoki and Snuke love cookies. They have A, B and ...
- A@GC*014
A@GC*014 A Cookie Exchanges 卡时跑了1s就输出-1 每次操作会使三个数的极差缩小一半,所以最多\(\log\)次之后就会出现\(A=B=C\)的情况,可以直接判掉 B Un ...
- Python Web学习笔记之Cookie,Session,Token区别
一.Cookie,Session,Token简介 # 这三者都解决了HTTP协议无状态的问题 session ID or session token is a piece of data that i ...
- 超大 Cookie 拒绝服务攻击
有没有想过,如果网站的 Cookie 特别多特别大,会发生什么情况? 不多说,马上来试验一下: for (i = 0; i < 20; i++) document.cookie = i + '= ...
- IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题
你是否遇到过当使用一个涉及到Cookie操作的网站或者管理系统时,IE 6.7.8.9下都跑的好好的,唯独到了IE10.11这些高版本浏览器就不行了?好吧,这个问题码农连续2天内遇到了2次.那么,我们 ...
- 解决cookie跨域访问
一.前言 随着项目模块越来越多,很多模块现在都是独立部署.模块之间的交流有时可能会通过cookie来完成.比如说门户和应用,分别部署在不同的机器或者web容器中,假如用户登陆之后会在浏览器客户端写入c ...
随机推荐
- hibernate3缓存(hibernate)
一级缓存:当应用程序调用Session 的save() .update() .savaeOrUpdate() .get() 或load() ,以及调用查询接口的list() .iterate() 或f ...
- shell脚本,如何用shell打印金字塔
- bcdboot应用
1.下个win8 的pe,功能齐全的.2.CMD执行命令 bcdboot c:\windows /s x: /f all c代表c盘即win所在分区盘符.s,命令参数,引导另存到其他地方.x,某储存引 ...
- [LUOGU] P2543 [AHOI2004]奇怪的字符串
LCS //Writer:GhostCai && His Yellow Duck #include<iostream> #include<cstring> #d ...
- js事件,操作页面文档,计算后样式,数据类型
js:运行在浏览器的脚本语言 js引入 1.行间式:存在于行间事件中 <div id="div" onclick="this.style.color="r ...
- include/autoconfig.mk
把autoconfig.mk和/include/configs/ $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/commo ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- MIP启发式求解:局部搜索 (local search)
*本文主要记录和分享学习到的知识,算不上原创. *参考文献见链接. 本文讲述的是求解MIP问题的启发式算法. 启发式算法的目的在于短时间内获得较优解. 个人认为局部搜索(local search)几乎 ...
- UIDatePicker 显示时间和打印时间不一样
默认是时区为0,如下格式化一下就好啦 UIDatePicker *datePiker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, ...
- java编程思想阅读记录
第五章:初始化与清理 1.构造器确保初始化 构造器采用与类名相同的方法. 创建对象时,将会为对象分配存储空间,并调用相应的构造器.这就确保了在你能操作对象之前,它就已经恰当的被初始化了. 垃圾回收器负 ...