Codeforces Round #295 (Div. 2) -- B. Two Buttons (模拟)
2015-03-03 18:25
519 查看
B. Two Buttons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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?
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 test(s)
input
output
input
output
Note
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.
思路:只能对n进行减1和乘2的操作来得到m,模拟一下即可,我是将m进行加1和除2的操作得到n;
AC代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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?
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 test(s)
input
4 6
output
2
input
10 1
output
9
Note
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.
思路:只能对n进行减1和乘2的操作来得到m,模拟一下即可,我是将m进行加1和除2的操作得到n;
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { int n, m, ans; while(scanf("%d %d", &n, &m) != EOF) { if(m <= n) { printf("%d\n", n - m); continue; } ans = 0; while(n != m) { int p = (m + 1) / 2; if(p < n) { if(m & 1) ans++; ans += (n - p); ans++; break; } else if(p >= n) { if(m & 1) ans++; ans++; m = (m + 1) / 2; } } printf("%d\n", ans); } }
相关文章推荐
- Codeforces Round #295 (Div. 2)——B——Two Buttons
- 模拟 Codeforces Round #203 (Div. 2) C. Bombs
- 【Codeforces Round 354 (Div 2)B】【简单模拟】Pyramid of Glasses 漏斗漏水n层t秒灌满多少个
- Codeforces Round #352 (Div. 2)(A)模拟
- Codeforces Round #Pi (Div. 2) B. Berland National Library (模拟)
- Codeforces Round #360 (Div. 2) 前三题题解【简单模拟+思维+二分图判定二分染色】
- Codeforces Round #344 (Div. 2) B. Print Check (模拟)
- Codeforces Round #335 (Div. 2) 606B Testing Robots(模拟)
- Codeforces Round #343 (Div. 2) -A. Far Relative’s Birthday Cake(组合+模拟)
- Codeforces Round #279 (Div. 2) B. Queue 模拟
- 【Codeforces Round 375 (Div 2) B】【水模拟】Text Document Analysis
- Codeforces Round #195 (Div. 2) / 336A Vasily the Bear and Triangle(模拟&数学)
- Codeforces Round #295 (Div. 2) E. Pluses everywhere
- Codeforces Round #354 (Div. 2) B (模拟)
- Codeforces Beta Round #35 (Div. 2) / 35A Shell Game(模拟)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)-模拟-C. Jury Marks-
- Codeforces Round #376 (Div. 2) A. Night at the Museum【模拟】
- 【Codeforces Round 334 (Div 2)A】【模拟 水题】Uncowed Forces CF算分模拟
- Codeforces Round #392 (Div. 2) A B C 水 模拟 暴力
- Codeforces Round #377 (Div. 2) C 模拟