J - Two Buttons

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

Description

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 Input

Input
4 6
Output
2
Input
10 1
Output
9

Hint

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.

出错点:没加标记数组,没有看数据限制范围。

广搜:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node{ int out;
int dep;
}pos;
const int maxn=11000;
bool mark[maxn];
queue<node>Q;
int aim;
int bfs(int n){ memset(mark,0,sizeof(mark));
pos.out=n;
pos.dep=0;
Q.push(pos);
mark[pos.out]=1;
while(!Q.empty()){ node tmp=Q.front();
Q.pop();
if(tmp.out!=aim){ node tmp1,tmp2;
tmp1.dep=tmp.dep+1;
tmp1.out=tmp.out*2;
if(tmp1.out>=1&&tmp1.out<=10000&&!mark[tmp1.out]){ Q.push(tmp1);
mark[tmp1.out]=1;
}
tmp2.out=tmp.out-1;
tmp2.dep=tmp.dep+1;
if(tmp2.out>=1&&tmp2.out<=10000&&!mark[tmp2.out]){ Q.push(tmp2);
mark[tmp2.out]=1;
}
}else{ return tmp.dep;
}
}
}
int main(){ int n,m;
while(scanf("%d%d",&n,&m)!=EOF){ while(!Q.empty()){ Q.pop();
} aim=m;
int ans;
if(n==aim){ printf("0\n");
}else{ ans=bfs(n);
printf("%d\n",ans);
}
}
return 0;
}

规律:可以逆向思考。n-1在某种意义上等同于m+1,n*2等同于m/2。

#include<stdio.h>
int main(){ int n,m;
while(scanf("%d%d",&n,&m)!=EOF){ int cnt=0;
if(n>=m){ printf("%d\n",n-m);
}else{ while(n!=m){ if(m&1){ m+=1;
cnt++;
}
m/=2;
cnt++;
if(n>m){ cnt+=n-m;
break;
}
}
printf("%d\n",cnt);
}
}
return 0;
}

  

CF520B——Two Buttons——————【广搜或找规律】的更多相关文章

  1. PIGS POJ - 1149网络流(最短增广路---广搜) + 建图

    题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...

  2. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  3. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  4. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  5. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  6. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 队列&广搜

    搜索里有深搜,又有广搜,而广搜的基础就是队列. 队列是一种特殊的线性表,只能在一段插入,另一端输出.输出的那一端叫做队头,输入的那一端叫队尾.是一种先进先出(FIFO)的数据结构. 正经的队列: 头文 ...

  9. 什么时候用深搜(dfs)什么时候用广搜(bfs)(转)

    1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止.这个时候不适宜使 ...

随机推荐

  1. sp_helptext输出错行问题解决

    相信,大家对sp_helptext存储过程一定不陌生,它可以帮你快速获取存储过程等对象的定义.但它有一个致命的缺点就是:每行最多返回255个nvarchar类型的字符,假如有一个编写不规范的存储过程, ...

  2. solr入门教程-较详细

    Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试;两个核心配置文件介绍;维护索引;查询索引,和在 ...

  3. CharSequence与String的区别

    CharSequence与String都能用于定义字符串,但CharSequence的值是可读可写序列,而String的值是只读序列. 原文: http://blog.csdn.net/joy_zha ...

  4. SFML从入门到放弃(2) 图像和音频

    SFML从入门到放弃(2) 图像和音频 精灵 精灵(sf::Sprite)就是截取纹理(sf::Texture)的一块 或者重复纹理贴图 初始化精灵和纹理的一些方法: sf::Sprite init_ ...

  5. ASP.NET Core中使用自定义验证属性控制访问权限

    在应用中,有时我们需要对访问的客户端进行有效性验证,只有提供有效凭证(AccessToken)的终端应用能访问我们的受控站点(如WebAPI站点),此时我们可以通过验证属性的方法来解决. 一.publ ...

  6. [Java]去除html中的标签或者元素属性(正则表达式)

    后台的数据库中某个字段是富文本框输入的 带有Html的标签 ,去掉标签后返回给前台 1.去掉Html 标签的代码 //过滤html标签 Pattern p_html = Pattern.compile ...

  7. appium +android例子

    配置文件: # coding:utf-8 __author__ = 'Helen' """ description:配置全局参数 """ i ...

  8. [转] Gitlab 8.x runner安装与配置

    [From]http://muchstudy.com/2018/07/13/Gitlab-8-x-runner%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE ...

  9. 论文阅读 | CenterNet:Object Detection with Keypoint Triplets

    相关链接 论文地址:https://arxiv.org/abs/1904.08189 代码链接:https://github.com/Duankaiwen/CenterNet 概述 CenterNet ...

  10. checkbox 框 选中判断

    function checkAll(checktop){ $(":checkbox[name='id']").prop("checked",checktop.c ...