首页 > 代码库 > 【BFS】Power Hungry Cows
【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:
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.
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 longinline 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。