题目链接

\(Description\)

给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2。求最少需要多少次可以使\(n\)等于\(m\)。

\(Solution\)

暴力连边BFS或者DP都行,都是O(n)的。有更优的做法。

过程是可逆的,我们考虑m变成n,有两种操作:1是m+=1;2是当m为偶数时,m/=2。

要用最少的次数使得m<=n。

因为m加两次再除以二和先除以二再加一次得到的结果是一样的,即能除就不加。这样就O(logn)解决了。

正推n到m好像推不出性质啊。。正推不行就试试倒推吧。

#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar() inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
} int main()
{
int n=read(),m=read(),ans=0;
for(; m>n; m>>=1, ++ans)
if(m&1) ++m, ++ans;
printf("%d\n",ans+n-m); return 0;
}

Codeforces.520B.Two Buttons(正难则反)的更多相关文章

  1. Codeforces 870F - Path(数论+分类讨论+正难则反)

    Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(d(u,v)\) 是个什么东西,分情况讨论: \(u\not\perp v\),\(d(u,v)=1\) \(u\perp ...

  2. FZU 2122 又见LKity【字符串/正难则反/KMP/把一个字符串中某个部分替换为另一个部分】

    嗨!大家好,在TempleRun中大家都认识我了吧.我是又笨又穷的猫猫LKity.很高兴这次又与各位FZU的ACMer见面了.最近见到FZU的各位ACMer都在刻苦地集训,整天在日光浴中闲得发慌的我压 ...

  3. P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)

    P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...

  4. Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】

    A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...

  5. Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】

    A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. CodeForces 520B Two Buttons(用BFS)

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

  7. CF 1005B Delete from the Left 【模拟数组操作/正难则反】

    You are given two strings s and t. In a single move, you can choose any of two strings and delete th ...

  8. CodeForces 520B Two Buttons

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

  9. 树上统计treecnt(dsu on tree 并查集 正难则反)

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

随机推荐

  1. Unity UGUI动态生成控件

    一. 首先你得先清楚RectTransform组件的一些程序控制 1. 先得到UGUI控件上面的RectTransform组件 RectTransform rtr = gameObject.GetCo ...

  2. hdu 6197 array array array LIS

    正反跑一次LIS,取最大的长度,如果长度大于n-k就满足条件. ac代码: #include <cstdio> #include <cstring> #include < ...

  3. svn之合并分支

    学习连接 svn的merge使用例子

  4. R语言做逻辑回归

    前面写过一个多分类的逻辑回归,现在要做一个简单的二分类,用glm函数 导入csv格式如下: mydata<-read.csv("D://li.csv",header=T) c ...

  5. GDB数据库SQL操作平台

    GDB数据库SQL操作平台 开发本软件的初衷:由于计算数据库要素层属性的时候,涉及到要计算多个字段,或者要根据代码计算名称,得一个一个的筛选并计算,过程比较繁琐,于是就想能不能通过像处理SQLServ ...

  6. .net core mvc + mysql dbfirst 生成 ado.net 数据模型

    1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 安装一下包 Install-Package MySql.Data.EntityFrameworkCore -Pre I ...

  7. 如何禁止Chrome浏览器隐藏URL的WWW前缀

    如何禁止Chrome浏览器隐藏URL的WWW前缀 一.打开Chrome浏览器 二.在Chrome浏览器的地址栏中输入以下内容并回车: chrome://flags/#omnibox-ui-hide-s ...

  8. linux串口命令

    proc # cat /proc/tty/driver/serial serinfo:1.0 driver revision: 0: uart:16550A port:000003F8 irq:4 t ...

  9. QCamera : no such file 问题

    在项目 *.pro中添加即可. QT += core gui QT += multimedia QT += multimediawidgets QT += multimedia ==>对应< ...

  10. 你不知道的javascript(上卷)读后感(二)

    this词法 熟悉ES6语法的开发者,箭头函数在涉及this绑定时的行为和普通函数的行为完全不一致.跟普通this绑定规则不一样,它使用了当前的词法作用域覆盖了this本来的值. 误解 this理解成 ...