zoukankan      html  css  js  c++  java
  • 【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

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

      

  • 相关阅读:
    分解质因数算法
    js 的 Math 对象
    字符串操作
    简化求质数算法
    数值类型小数点后是否可以接零问题
    新博第一篇,思考的重要性与求质数算法
    一、制作屏幕录像
    四、同步线程
    常见问题
    jni数据处理
  • 原文地址:https://www.cnblogs.com/wxjor/p/6958661.html
Copyright © 2011-2022 走看看