标签:numbers sdi stack tar number one problem imu stdout
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5522 | Accepted: 1384 |
Description
WV1 WV2
Start: x 1
Multiply first by first, store in second: x x^2
Multiply second by second: x x^4
Multiply second by second: x x^8
Multiply second by second: x x^16
Multiply second by second: x x^32
Divide second by first: x x^31
Input
Output
Sample Input
31
Sample Output
6
Source
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<stack> #include<vector> #include<algorithm> //#include<cmath> using namespace std; const int INF = 9999999; #define LL long long inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘; return x*f; } int N,M,K; bool vis[201][200001]; struct data{ int x,y,st; }Que[20000001]; bool flag=false; int l=1,r=1; int ans; void dfs(int a,int b,int c){ if(a>b) swap(a,b); if(flag||a>200||b>20000) return ;//这里要判一下最优 if(!vis[a][b]){ vis[a][b]=true; Que[++r].x=a,Que[r].y=b; Que[r].st=c; if(!flag&&(a==K||b==K)){ flag=true; printf("%d\n",c); } } } void bfs(){ Que[1].x=0,Que[1].y=1,Que[1].st=0; int step; while(l<=r){ step=Que[l].st,N=Que[l].x,M=Que[l].y; dfs(N,N+N,step+1); dfs(N,M+M,step+1); dfs(M+M,M,step+1); dfs(N+N,M,step+1); dfs(N,M+N,step+1); dfs(N+M,M,step+1); dfs(M-N,M,step+1); dfs(N,M-N,step+1); l++; if(flag) return ; } } int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); K=read(); bfs(); return 0; }
标签:numbers sdi stack tar number one problem imu stdout
原文地址:http://www.cnblogs.com/wxjor/p/6958661.html