codeforces 520 Two Buttons
http://codeforces.com/problemset/problem/520/B
2 seconds
256 megabytes
Vasya has found a 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?
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .
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.
4 6
2
10 1
9
In the first example you need to push the blue button once, and then push the red button once.
In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
分析:
直接模拟,从后面往前面推。
AC代码:
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma warning(disable:4786) using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = + ;
const double eps = 1e-;
const double PI = acos(-1.0); int main()
{
int n , m;
while(~scanf("%d %d",&n , &m))
{
int ans = ;
while(n < m)
{
if(m % )
{
m ++;
ans ++;
}
else
{
m /= ;
ans ++;
}
}
cout << ans + n - m << endl;
}
return ;
}
codeforces 520 Two Buttons的更多相关文章
- codeforces 520 Pangram
http://codeforces.com/problemset/problem/520/A A. Pangram time limit per test 2 seconds memory limit ...
- CodeForces 520B Two Buttons(用BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round B. Buttons
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...
- CodeForces 520B Two Buttons
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Vasya ...
- Codeforces.520B.Two Buttons(正难则反)
题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...
- Codeforces 520B:Two Buttons(思维,好题)
题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...
- Codeforces Round #520 (Div. 2)
Codeforces Round #520 (Div. 2) https://codeforces.com/contest/1062 A #include<bits/stdc++.h> u ...
- CF 520 B. Two Buttons(bfs)
/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步.可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,就变成原来的数.CF 520 B. Two Buttons思 ...
- 【codeforces 520B】Two Buttons
[题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...
随机推荐
- maven 添加本地jar
方式一 Xml代码 <dependency> <groupId>org.apache</groupId> <artifactId>test</ar ...
- [dpdk] 读官方文档(2)
续前节.切好继续: 一,文档里提到uio_pci_generic, igb_uio, vfio_pci三个内核模块,完全搞不懂,以及dpdk-devbind.py用来查看网卡状态,我得到了下边的输出: ...
- Java程序员从笨鸟到菜鸟之(二十一)java过滤器和监听器详解 【转】
过滤器 1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请 ...
- $watch、$digest、$apply
$watch.$digest.$apply $watch 代表的就是对数据源的监听,当数据源发生变化,就会触发第二个参数的回调函数 $digest 代表触发一个数据源变化的事件 $apply 代表对于 ...
- 【Java 进阶篇】【第二课】异常处理
概念 异常处理的存在是为了: 允许程序员跳过无法处理的问题,继续开发后续的工作,或根据异常做出更加聪明的处理方式. Java中存在一类对象叫“异常对象”. 当异常情况发生时,就会暗战预先的设定,抛 ...
- 逻辑运算符——逻辑与&&、逻辑或||
一直以来,都是认为逻辑运算符返回的是布尔值,却突然发现:并不是这样. 对于||来说,如果条件判断结果为true就返回第一个操作数的值,如果为false就返回第二个操作数的值. &&则相 ...
- 一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现。
一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现.
- php--tp3.2引入sphinx搜索
1.首先我们把coreseek下载好,命名为coreseek,我们找到coreseek/etc中的csft_mysql.conf修改这个配置文件 #源定义 source lemai { type ...
- 设计模式:抽象工厂模式(Abstract Factory)
定 义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 结构图: 示例结构图: 实体类: class User { public int Id { get; set; } p ...
- mysql integer size 大小
I was always wondering what the size of numeric columns in MySQL was. Forgive me if this is obvious ...