codeforces520B-Two Buttons
来源:互联网 发布:如何自己做软件 编辑:程序博客网 时间:2024/06/05 12:03
题意:两个数n和m,n可以乘2也可以减1,问n最少进行几次操作可以得到m。
如果n>m显然答案是n-m。
如果n<m,那么我们可以逆着来,用m去凑n,想一想如果m为奇数,那一定是n最后-1得到的,如果m为偶数,那一定是乘2得到的。
那么当m为奇数我们可以让它+1,当m为偶数我们可以让他/2,知道m小于n,那么我们再加上(n-m)就是我们要求的答案。
#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>using namespace std;struct node{int x,step;};int main(){int a,b;cin>>a>>b;if(a>b){printf("%d\n",a-b);}else{int ans=0;while(b>a){if(b&1) b=(b+1)/2,ans+=2;else b/=2,ans++;}ans+=a-b;cout<<ans<<endl;}return 0;}
阅读全文
0 0
- codeforces520B-Two Buttons
- codeforces520B-Two Buttons
- 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
- 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
- join联查
- [Algorithm]排序原理
- Android 三指截屏
- OA权限管理
- 【机器学习笔记】SVD分解
- codeforces520B-Two Buttons
- java web 之自定义校验
- Javase—抽象类与接口习题
- 命令模式
- Charles抓包工具使用介绍
- 汇编实验报告
- 【2017年11月27日】CSDN博客更新周报
- POJ 1087
- CodeForces