题目链接: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. Java获取路径(getResource)

    package com.zhi.test; public class PathTest { public static void main(String[] args) { System.out.pr ...

  2. Latex 循环插图、文件名包含空格

    \,...,} { \begin{figure} \centering \includegraphics[width=10cm]{pictures//rela\n} \caption{Attribut ...

  3. codeigniter在nginx下返回404 not found

    codeigniter框架需要path_info的支持,Apache默认支持path_info,但是nginx默认不支持,我们需要设置nginx,使得nginx支持path_info 网上试了好多方法 ...

  4. JQuery复制内容到剪切板-jquery.zclip.js的使用,在公司项目中

    公司项目中有一个复制粘贴的内容,也试图找其他插件但都是浏览器兼容问题,在网上找这个插件挺不错的,FLASH,兼容各个浏览器,测试时要在服务器环境下,点击参考,参考这个网址,或者搜下标题这个插件,性能不 ...

  5. java将字符串根据空格进行分割,使用split方法

    public class D { public static void main(String[] args) { String b = "Hello Java World"; S ...

  6. unity鼠标滚轮控制摄像机视野的缩放和按住鼠标控制摄像机移动

    //摄像机前进后退的速率 private float view_value=20f; private float maximum = 100; private float minmum = 30; / ...

  7. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

  8. CentOS7下安装MySQL5.7安装与配置

    介绍在CentOS7上yum安装数据库服务器MySQL Community Server 5.7的方法. 准备 CentOS7默认安装了和MySQL有兼容性的MariaDB数据库,在我们安装MySQL ...

  9. 3.4 C++名字隐藏

    参数:http://www.weixueyuan.net/view/6361.html 总结: 如果派生类中新增一个成员变量,该成员变量与基类中的成员变量同名,则新增的成员变量就会遮蔽从基类中继承过来 ...

  10. html页面小技巧

    #1.onkeyup限制输入框只能输入数字 通过onkeyup事件是输上后再去掉非数字字符 <input type="text" onkeyup="value=va ...