第十四届华中科技大学程序设计竞赛--J Various Tree
链接:https://www.nowcoder.com/acm/contest/106/J
来源:牛客网
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
1. y=x+1
2. y=x-1
3. y=x+f(x)
The function f(x) is defined as the number of 1 in x in binary representation. For example, f(1)=1, f(2)=1, f(3)=2, f(10)=2.
输入描述:
One line with two integers A and B
, the init type and the target type.
输出描述:
You need to print a integer representing the minimum steps.
输入例子:
5 12
输出例子:
3
-->
输入
5 12
输出
3
说明
The minimum steps they should take: 5->7->10->12. Thus the answer is 3. 开始以为广搜会t,然而并不会
/*
data:2018.5.20
author:gsw
link:https://www.nowcoder.com/acm/contest/106#question
*/
#define ll long long
#define IO ios::sync_with_stdio(false); #include<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1000005 int brainy[maxn];
int vis[maxn];
int getbrainy(int a)
{
int ans=;
while(a>)
{
if(a&)ans++;
a=a>>;
}
return ans;
}
void init()
{
for(int i=;i<maxn;i++)
brainy[i]=getbrainy(i);
memset(vis,,sizeof(vis));
}
class Step
{
public:
int x,st;
};
void bfs(int a,int b)
{
Step be,ne;
be.x=a;be.st=;
queue<Step> q;
q.push(be);
vis[be.x]=;
while(!q.empty())
{
be=q.front();
q.pop();
if(be.x==b)
{
cout<<be.st<<endl;
return;
} if(!vis[be.x+])
{
vis[be.x+]=;
ne.x=be.x+;ne.st=be.st+;
q.push(ne);
}
if((be.x-brainy[be.x])>=&&!vis[be.x-brainy[be.x]])
{
vis[be.x-brainy[be.x]]=;
ne.x=be.x-brainy[be.x];ne.st=be.st+;
q.push(ne);
}
if((be.x-)>=&&!vis[be.x-])
{
vis[be.x-]=;
ne.x=be.x-;ne.st=be.st+;
q.push(ne);
}
if(!vis[be.x+brainy[be.x]])
{
vis[be.x+brainy[be.x]]=;
ne.x=be.x+brainy[be.x];ne.st=be.st+;
q.push(ne);
}
}
}
int main()
{
int a,b;
init();
scanf("%d%d",&a,&b);
bfs(a,b);
}
第十四届华中科技大学程序设计竞赛--J Various Tree的更多相关文章
- 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees
A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)
链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...
- 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest
链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...
- 第十四届中北大学ACM程序设计竞赛 J.ZBT的游戏
问题描述 第14届中北大学程序设计竞赛来了,集训队新买了一大堆气球,气球一共有K种颜色(1<=K<=256),气球的颜色从1-K编号. ZBT童心未泯,他发明了一种摆放气球的游戏,规则如下 ...
随机推荐
- [CSP-S模拟测试]:抽卡(概率DP)
题目描述 水上由岐最近在肝手游,游戏里有一个氪金抽卡的活动.有$n$种卡,每种卡有 3 种颜色.每次抽卡可能什么也抽不到,也可能抽到一张卡.每氪金一次可以连抽 m 次卡,其中前$m−1$次抽到第$i$ ...
- 通过泛型,将string转换为指定类型
Generic TryParse You should use the TypeDescriptor class: public static T Convert<T>(this stri ...
- 【C++第一个Demo】---控制台RPG游戏2【通用宏、背包类】
[通用 ]--一些游戏中常用的宏.函数和枚举 #ifndef _MARCO_H_ #define _MARCO_H_ //------------------------常用系统库---------- ...
- NTFS文件系统
一.Volume和Cluster 卷(Volume)和簇(Cluster)是NTFS用来描述物理磁盘的单位. 卷之间是相对独立的,卷的概念其实就是分区(Partition). 簇的引入是为了方便处理不 ...
- AVL树,C语言实现,完整代码,先贴上,讲解稍后
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #de ...
- 浅析Reactor设计模式
简介:Reactor 设计模式是一种事件驱动的设计模式,将一个或者多个客户端请求分发到不同的处理器上,来提升事件处理的效率.主要的应用场景就是java NIO当中用户处理网络请求.使用的是异步非阻塞I ...
- POI教程
很多时候,一个软件应用程序需要生成Microsoft Excel文件格式的报告.有时,一个应用程序甚至希望将Excel文件作为输入数据.例如,一个公司开发的应用程序将财务部门需要所有输出生成自己的Ex ...
- java8 Date LocalDate LocaDateTime 互相转化
java 8中 java.util.Date 类新增了两个方法,分别是from(Instant instant)和toInstant()方法 // Obtains an instance of Dat ...
- Windows10安装好Visual Studio2017后,找不到MFC向导
前段时候在Windows10中安装好Visual Studio2017后,想创建一个基于MFC的对话框应用,发现无法找到MFC开发向导选项,很是奇怪,以前使用VC6.0或者Visual Studio2 ...
- react textArea 高度不变化问题
ref={el =>{ if(el){ el.textareaRef.style.height = el.textareaRef.scrollHeight + 'px'; } } }