Codeforces 520B:Two Buttons(思维,好题)
题目链接:http://codeforces.com/problemset/problem/520/B
题意
给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m
思路
在百度之前用了各种方法,dfs,递推什么的能用的全用了,最后都WA在了第七组
百度了一下,看到了大佬们的代码震精了!!!竟然只有一行!!
用逆推,把从n变成m转换成从m变成n。
如果m是偶数那么m一定是通过上一步乘2的操作变来的,如果是奇数,那么一定是通过上一步减一变来的。最后当m小于n的时候,停止循环,令循环过程中得到的操作次数加上n和m的差值就是正确结果了
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
int res=0;
while(n<m)
{
if(m%2)
m++;
else
m/=2;
res++;
}
cout<<res+n-m<<endl;
return 0;
}
Codeforces 520B:Two Buttons(思维,好题)的更多相关文章
- CodeForces 520B Two Buttons(用BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CodeForces 520B Two Buttons
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Vasya ...
- codeforces 804A Find Amir 思维/水题
A few years ago Sajjad left his school and register to another one due to security reasons. Now he w ...
- Codeforces.520B.Two Buttons(正难则反)
题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...
- http://codeforces.com/gym/100623/attachments E题
http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...
- [Gym101982M][思维好题][凸壳]Mobilization
[gym101982M][思维好题][凸壳]Mobilization 题目链接 20182019-acmicpc-pacific-northwest-regional-contest-div-1-en ...
- http://codeforces.com/gym/100623/attachments H题
http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...
随机推荐
- jQuery html5Validate基于HTML5表单 异步服务器端验证
1. HTML5 自带的Validate 很漂亮,很好用, 但是一定要在form里用submit按钮,才生效 <form id="frmInfo" action=" ...
- static与全局变量区别
作用域不同: 全局变量是不显式用static修饰的全局变量,但全局变量默认是动态的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过extern 全局变量名的声明,就可以使用全局变量 ...
- BIOS、BootLoader、uboot对比
BIOS BIOS是英文"Basic Input Output System"的缩略语,直译过来后中文名称就是"基本输入输出系统".其实,它是一组固化到计算机内 ...
- win10与centos7的双系统U盘安装(二:安装以及配置centos系统)
继续第一篇的讲解,接着就是要进入第二部分——安装以及配置centos系统 特别提醒在安装centos之间,注意自己的win10系统进行备份,便于回复安装失败后的系统 1:安装前准备 安装前准备主要是两 ...
- sass 变量的声明 嵌套
sass 的默认变量一般是用来设置默认值,然后根据需求来覆盖的,覆盖的方式也很简单,只需要在默认变量之前重新声明下变量即可. $baseLineHeight: 2; $baseLineHeight: ...
- :组合模式:Component
#ifndef __COMPONENT_H__ #define __COMPONENT_H__ #include <iostream> #include <vector> us ...
- SharePoint Framework 在Visual Studio Code中调试你的托管解决方案
博客地址:http://blog.csdn.net/FoxDave 上一篇介绍了如何在本地调试你的SharePoint Framework解决方案,本篇介绍如何调试你的SharePoint Onl ...
- CodeForces - 779D
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But i ...
- 禁止textarea拉伸
添加css属性: style="resize:none" ;
- L266 作文
With the rapid development of modem society, an increasing number of people are concerned about the ...