Two Buttons
来源:互联网 发布:云和大数据 编辑:程序博客网 时间:2024/06/05 06:08
Two Buttons
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
DescriptionVasya hasfounda strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
Input
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space.
Output
Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.
Sample Input
Input
4 6
Output
2
Input
10 1
Output
9
题目简化大意:给你两个数N,M,对于N有两种操作--减一和乘二
求从N变换到M的最少操作次数(每次只能进行一种操作)
例如N=4,M=6时,最少操作次数为2,即(4-1)*2=6;
N=10,M=1时,N只能进行减的操作,需要减9次
解题思路:开始的时候没有往搜索方向想,想了好久也没有
推出什么公式来,后来在稿纸左画画右画画突然灵机一动,不是
直接可以暴搜一顿吗?这不直接就出来了?然后键盘啪啪啪,代码
出来了,结果就悲剧了~~超内存有木有!没有剪枝啊!直接搜不加优化
的话会浪费系统运行时间和内存空间,所以啊,苦逼的剪枝来了~~~
///下面这段代码是没有经过剪枝的,直接把不等于M的数都放入队列中,结果嘛,可以粘到
///编译器上试试~~(可以试试1234 4321这组数据)
///后来剪啊剪,把等于0,大于M的,和重复的数都剪掉,才AC掉
- Two Buttons
- Two Buttons
- Two Buttons
- Two Buttons
- Two Buttons
- Two Buttons
- Two Buttons(BFS)
- B. Two Buttons
- CODEFORCES:B. Two Buttons
- B. Two Buttons
- codeforces520B-Two Buttons
- codeforces520B-Two Buttons
- CodeForces 520B【Two Buttons】
- Codeforces 520B. Two Buttons
- Codeforces 520B.Two Buttons
- B. Two Buttons(Codeforces Round #295(div2))
- Codeforces Round #295 B. Two Buttons
- Codeforces 520B. Two Buttons spfa
- 黑马程序员——Foundation之结构体
- 对指针的深彻透析
- C#处理MySql多个返回集的方法
- 安卓处理机制之onTouchaction
- HDU4869:Turn the pokers[错误分析
- Two Buttons
- 工作流入门链接+分享本书《Activiti实战》
- 基本的导航
- Cookie学习总结-显示上一次访问时间
- 泛型第二课,派生子类、属性类型、方法重写、泛型擦除
- nyoj1178 异星人
- 面向对象的基本概念(一)
- 【数学】Excel Sheet Column Title
- 手工创建Oracle遇到的几个错误及解决方案