FZU 2147 A-B Game
来源:互联网 发布:淘宝版网页版登录 编辑:程序博客网 时间:2024/05/29 14:22
Description
Fat brother and Maze are playing a kind of special (hentai) game by two integers A and B. First Fat brother write an integer A on a white paper and then Maze start to change this integer. Every time Maze can select an integer x between 1 and A-1 then change A into A-(A%x). The game ends when this integer is less than or equals to B. Here is the problem, at least how many times Maze needs to perform to end this special (hentai) game.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers A and B described above.
1 <= T <=100, 2 <= B < A < 100861008610086
Output
For each case, output the case number first, and then output an integer describes the number of times Maze needs to perform. See the sample input and output for more details.
Sample Input
25 310086 110
Sample Output
Case 1: 1Case 2: 7
解析
这个题找贪心,每次变换后的数字要尽量小,那么x尽量接近A/2。若A为偶x=A/2+1,若A为奇x=(A+1)/2。
等式变换一下就有:
if(a%2==0) a=(a>>1)+1;else a=(a+1)>>1;
代码:
#include<iostream>#include<cstdio>using namespace std;int T;void work(){long long a,b;//scanf("%I64d%I64d",&a,&b);cin >> a >>b; int time=0;while(a>b){time++;if(a%2==0) a=(a>>1)+1;else a=(a+1)>>1;}printf("%d\n",time);}int main(){freopen("HABGame.in","r",stdin);freopen("HABGame.out","w",stdout);scanf("%d",&T);for(int i=1;i<=T;i++){printf("Case %d: ",i);work();}return 0;}
- FZU 2147 A-B Game
- FZU 2147 A-B Game
- FZU-2147 A-B Game
- FZU 2147A-B Game
- FZU 2147 A-B Game
- FZU-Problem 2147 A-B Game
- 【瞎搞】 FZU 2147 A-B Game
- FZU - 2147 A-B Game(找规律)
- FZU-2147-2147 A-B Game,规律题。。
- FZU Problem 2147 A-B Game (数学啊)
- FZU OJ 2147 A-B Game (数学水题)
- FZU 2147 A-B Game(数学推导题)
- fzu 2146 Easy Game & 2147 A-B Game & 2151 OOXX Game(水题)
- FZU A-B problem
- Another a+b FZU
- Foj 2147 A-B Game
- Problem 2147 A-B Game
- B OOXX Game(FZU 2151)
- Spark1.0.x入门指南(spark on yarn, standalone)
- 简述设计模式及设计原则
- 精通安卓性能优化-第五章(二)
- swf破解扫盲教程-内存抓取法破解swf加壳
- C#反射之Assembly.Load,Assembly.LoadFile 与 Assembly.LoadFrom方法介绍
- FZU 2147 A-B Game
- 验证java 递增不是原子操作
- 题目1518:反转链表
- OJ D
- 2014MadCon厦门分享会-笔记(下)
- 两个老外婆过河,函数递归解法
- 根据设备及设备语言去访问不同的网址。(核心:navigator.userAgent、navigator.language)
- zookeeper使用和原理探究
- Spring中DispacherServlet、WebApplicationContext、ServletContext的关系