题目链接: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(思维,好题)的更多相关文章

  1. CodeForces 520B Two Buttons(用BFS)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. CodeForces 520B Two Buttons

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Vasya ...

  3. codeforces 804A Find Amir 思维/水题

    A few years ago Sajjad left his school and register to another one due to security reasons. Now he w ...

  4. Codeforces.520B.Two Buttons(正难则反)

    题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...

  5. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  6. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  7. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  8. http://codeforces.com/gym/100623/attachments E题

    http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...

  9. [Gym101982M][思维好题][凸壳]Mobilization

    [gym101982M][思维好题][凸壳]Mobilization 题目链接 20182019-acmicpc-pacific-northwest-regional-contest-div-1-en ...

  10. http://codeforces.com/gym/100623/attachments H题

    http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...

随机推荐

  1. FFmpeg点播慢的最终方案

    转载: 音视频交流群  发的一个总结.  原作者 请查看相关博客作者 http://blog.51cto.com/fengyuzaitu/2061036 场景要求 项目要求点播速度是300到500毫秒 ...

  2. Redis分布式锁实例

    maven依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</ ...

  3. 如何让一个div水平和垂直居中对齐

    以下方法来自百度知道:https://zhidao.baidu.com/question/558984366971173044.html 方法1: .parent { width: 800px; he ...

  4. 【性能测试工具ab】ab工具使用

    1.在安装了apache服务器后,或者wampserver后,在bin目录下,有一个ab.exe文件 2.使用,进入ab.exe所在的文件夹 3.ab -c   10 -n  1000     htt ...

  5. php 处理ftp常用操作与方法

    原文地址:https://www.cnblogs.com/longfeiPHP/p/5420632.html $ftp_conn = ftp_connect("192.168.1.230&q ...

  6. sql2008r2安装失败的解决办法

    setup fails with: '.', hexadecimal value 0x00, is an invalid character.SQL 2012 Setup issues - hexad ...

  7. Unix分类学习

    调试 shell # bash -x script.sh 设置终端背景色 setterm -background black 一.网络 1.网卡状态 mii-tool -v ethtool eth0 ...

  8. ubuntu下修改文件权限

    参考:https://www.cnblogs.com/viviwind/archive/2012/08/02/2619451.html 常用方法如下: sudo chmod 600 ××× (只有所有 ...

  9. 在MATLAB中安装MinGW-w64 C/C++ 编译器的方法

    reference:http://blog.sina.com.cn/s/blog_167bbdec10102x113.html 在MATLAB中编译C/C++ 文件时出现以下情况: 说明缺少MinGW ...

  10. innodb mvcc,事务隔离级别,读写锁

    mvcc其实和copyonwritelist的思路差不多:读不加锁,写加锁,事务提交之后释放锁,并且延伸的是,在UNdolog里面保存了几个版本,实现不同的隔离级别.如果读数据页里面最新的数据,那么就 ...