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. IdentityServer4与ocelot实现认证与客户端统一入口

    关于IdentityServer4与ocelot博客园里已经有很多介绍我这里就不再重复了. ocelot与IdentityServer4组合认证博客园里也有很多,但大多使用ocelot内置的认证,而且 ...

  2. MySQL8.0本地访问设置为远程访问权限

    1.登录MySQL mysql -u root -p 输入您的密码 2.选择 mysql 数据库 use mysql; 因为 mysql 数据库中存储了用户信息的 user 表. 3.在 mysql ...

  3. 921. Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  4. js返回上一级代码和刷新页面代码

    返回上一级代码: <a href="javascript:;" onclick="history.go(-1);" class="icon_ba ...

  5. codeforces785E

    http://codeforces.com/contest/785/problem/E 一道经典的求逆序对的题目,可以用树状数组套平衡树解决 平衡树需要支持插入一个数,删除一个数,找比 x 小的数的个 ...

  6. 利用Logstash plugins做更多的事情

    1. 引言 之前一篇文章<Logstash 介绍及linux下部署>,我们实现了logstash的安装以及简单的控制台标准输入输出测试,那么logstash能不能做更多的事情呢?答案是肯定 ...

  7. Linux rsync 企业级应用

    简介 rsync 是 Linux 下的数据同步工具, 其支持本地同步和远程同步, 远程同步分为 daemon 和 ssh 同步方式    rsync 可以代替 cp, scp 等命令, 且具有更高的可 ...

  8. 一次http请求响应流程

    前端客户端 发起http请求 web服务器接收并解析http报文 通过WSGI协议发送给web框架 web框架创建请求对象 中间层处理 具体的视图处理-业务处理 中间层处理 创建http响应对象 返回 ...

  9. lexical or preprocessor issue file not found in Xcode

    The very last suggestion is all I needed to fix this issue. Close & re-open Xcode.

  10. C#找出接口的所有实现类并遍历执行这些类的公共方法

    //这里找出了实现IOutputArray接口的所有类 private void FindAllClass() { var types = AppDomain.CurrentDomain.GetAss ...