博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Atcoder 2373 Cookie Exchanges
阅读量:4588 次
发布时间:2019-06-09

本文共 1534 字,大约阅读时间需要 5 分钟。

Problem Statement

 

Takahashi, Aoki and Snuke love cookies. They have AB 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 412 and 20 cookies. Then,

  • After the first action, they have 1612 and 8.
  • After the second action, they have 1012 and 14.
  • After the third action, they have 1312 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
#define ll long longusing 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;}

  

 

转载于:https://www.cnblogs.com/JYYHH/p/8781615.html

你可能感兴趣的文章
打印图片,自动调整宽高
查看>>
对类使用dir()
查看>>
【13】淘宝sdk——入门实战之header.php制作(一)
查看>>
安装SoapUI Pro
查看>>
杜教BM模板
查看>>
Makefile经典教程(掌握这些足够)
查看>>
自己成功的编写的将数据从excel导入到access中
查看>>
【Leetcode】【Easy】Compare Version Numbers
查看>>
014 链表中倒数第k个结点
查看>>
Python的pip安装Django
查看>>
第一冲刺阶段——站立会议第二天4月19日
查看>>
hdu-----(2807)The Shortest Path(矩阵+Floyd)
查看>>
简洁的MysqlHelper
查看>>
Android面试收集录2 Broadcast Receiver详解
查看>>
基于HTML5实现的中国象棋游戏
查看>>
Luogu P2024 [NOI2001]食物链 | 并查集
查看>>
openLayers3 中实现多个Overlay
查看>>
SQlServer2008 之 定时执行sql语句作业的制定
查看>>
函数式编程
查看>>
由浅入深之Jquery笔记(1)
查看>>