【BFS】Power Hungry Cows

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

USACO 2002 February

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

  

时间: 2024-10-21 13:31:34

【BFS】Power Hungry Cows的相关文章

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs

【bfs】hdu 1104 Remainder

[bfs]hdu 1104 Remainder 题目链接:hdu 1104 Remainder 很不错的一道搜索题目,但是有几个关键问题要注意. 最短路径,果断bfs+Queue 路径的存储问题,之前只想把每一步的计算结果存储到queue(int)Q中,后来发现路径无法记录,就选择存储节点的方式并用string保存路径,queue(node)Q,开一个临时的节点node p,每进行一次运算就更新它的路径string+'op',最终输出的一定是完整路径!! 但最关键的是取模!!!!! discus

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok