标签:step 64bit lse www ble include 竞赛 names 分享
链接:https://www.nowcoder.com/acm/contest/106/J
来源:牛客网
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
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=0; while(a>0) { if(a&1)ans++; a=a>>1; } return ans; } void init() { for(int i=1;i<maxn;i++) brainy[i]=getbrainy(i); memset(vis,0,sizeof(vis)); } class Step { public: int x,st; }; void bfs(int a,int b) { Step be,ne; be.x=a;be.st=0; queue<Step> q; q.push(be); vis[be.x]=1; while(!q.empty()) { be=q.front(); q.pop(); if(be.x==b) { cout<<be.st<<endl; return; } if(!vis[be.x+1]) { vis[be.x+1]=1; ne.x=be.x+1;ne.st=be.st+1; q.push(ne); } if((be.x-brainy[be.x])>=0&&!vis[be.x-brainy[be.x]]) { vis[be.x-brainy[be.x]]=1; ne.x=be.x-brainy[be.x];ne.st=be.st+1; q.push(ne); } if((be.x-1)>=0&&!vis[be.x-1]) { vis[be.x-1]=1; ne.x=be.x-1;ne.st=be.st+1; q.push(ne); } if(!vis[be.x+brainy[be.x]]) { vis[be.x+brainy[be.x]]=1; ne.x=be.x+brainy[be.x];ne.st=be.st+1; q.push(ne); } } } int main() { int a,b; init(); scanf("%d%d",&a,&b); bfs(a,b); }
第十四届华中科技大学程序设计竞赛--J Various Tree
标签:step 64bit lse www ble include 竞赛 names 分享
原文地址:https://www.cnblogs.com/fantastic123/p/9063441.html