码迷,mamicode.com
首页 > 其他好文 > 详细

【BFS】Power Hungry Cows

时间:2017-06-07 20:51:33      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:numbers   sdi   stack   tar   number   one   problem   imu   stdout   

Power Hungry Cows
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5522   Accepted: 1384

Description

FJ‘s cows would like to be able to compute integer powers P (1 <= P <= 20,000) of numbers very quickly, but need your help. Because they‘re going to be computing powers of very large numbers, they can only keep around two work variables for intermediate results. 

The first of those work variables is initialized to the number (denoted x) for which they are calculating the power; the other is initialized to 1. The cows can both multiply and divide any pair of the work variables and store the result in any work variable, but all results are stored as integers. 

For example, if they want to compute x^31, one way to perform the calculation is: 
                                              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

Thus, x^31 can computed in six operations. Given the power to be computed and the the number of work variables, find the minimum number of operations to calculate the power. 

Input

A single line with one integer: P. 

Output

A single line with a single integer that is the minimum number of operations it requires to compute the power. 

Sample Input

31

Sample Output

6

Source

 
 
题目大意:两个容器,每次可以把两个相乘/相除放到某一个容器中,问最少几次能得到x^N? (poj1945)
 
代码
#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;
}

  

【BFS】Power Hungry Cows

标签:numbers   sdi   stack   tar   number   one   problem   imu   stdout   

原文地址:http://www.cnblogs.com/wxjor/p/6958661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!