【BFS搜索】poj1945 Power Hungry Cows
2015-07-25 16:44
399 查看
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
Multiply first by first, store in second: x x^2
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
题目大意:对(1,0)进行操作得到n,求最少操作次数。
第一反应是BFS搜索。当然也可以用A*算法
在实现过程中注意搜索顺序,否则就会RE,TLE。= =
因为担心用STL会超时就手写的队列,至于数组的大小嘛,经多次实验得到的……
具体操作见代码。
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
题目大意:对(1,0)进行操作得到n,求最少操作次数。
第一反应是BFS搜索。当然也可以用A*算法
在实现过程中注意搜索顺序,否则就会RE,TLE。= =
因为担心用STL会超时就手写的队列,至于数组的大小嘛,经多次实验得到的……
具体操作见代码。
#include <iostream> #include <cstdio> #include <cstdlib> #define MAXN 20110 #define MAXM 101 using namespace std; int n ,head ,last ,q[700003][3] ,n2 ; bool visit[MAXN][MAXM] ; bool add(int wv1,int wv2,int step) { if(wv1==n||wv2==n) return true; if(wv1<wv2)//将大的放在前面 { int temp=wv2; wv2=wv1 ,wv1=temp ; } if(wv1==wv2||wv1>=n2||wv2>=MAXM) return false; if(!visit[wv1][wv2]) { visit[wv1][wv2]=1; ++last; q[last][0]=wv1 ,q[last][1]=wv2 ,q[last][2]=step; } return false; } void bfs() { add(1,0,0); int wv1 ,wv2 ,step ; while(head<last) { ++head; wv1=q[head][0] ,wv2=q[head][1] ,step=q[head][2]+1 ; if(add(wv1+wv1,wv2,step)||add(wv1+wv2,wv2,step)||add(wv2+wv2,wv2,step)||add(wv1,wv1+wv1,step)||add(wv1,wv1+wv2,step)||add(wv1,wv2+wv2,step)||add(wv1,wv1+wv2,step)||add(wv1,wv1-wv2,step)||add(wv1-wv2,wv1,step)) { //这里有贪心的方法:将大的先入队 printf("%d\n",step); return; } } return; } int main() { scanf("%d",&n); n2=n+MAXM; bfs(); return 0; }
相关文章推荐
- SAP推出iPhone手机端企业智能管理应用
- 企业移动应用平台:走进SAP SUP的世界
- Collection接口和Collections类的简单区别和讲解
- MySQL-终端基本操作
- 共轭梯度法(conjugate gradient method)
- 广告过滤算法实现及优化
- CoreData入门例子
- Java多线程(二)、线程的生命周期和状态控制
- HDOJ-3949 XOR(高斯消元)
- [2015.07.27]万峰图片批量处理专家 v8.6
- ios入门之c语言篇——基本函数——2——判断闰年
- JavaScript中指针和地址理解
- .h .m切换
- UVa673 Parentheses Balance
- CF Gym 100637J Superfactorial numeral system (构造)
- CF Gym 100637K Microcircuits (DP)
- CF Gym 100637G \#TheDress (水)
- CF Gym 100637F The Pool for Lucky Ones
- CF Gym 100637B Lunch(拆分子问题)
- ios入门之c语言篇——基本函数——1——随机数生成