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;
    }
    

      

  • 相关阅读:
    JAVA LinkedList和ArrayList的使用及性能分析
    学习笔记—Node中的模块调试
    学习笔记—Node的核心模块
    学习笔记—Node中VM模块详解
    学习笔记—Node中require的实现
    入园了
    【引用】asp.net服务器推送(ServerPush)和客户端拉拽技术
    ajax xmlHttp.responseXML取不到值问题备忘
    oracle实时插值速度突然变慢问题解决办法
    [转帖 作者: fuyuncat 来源: www.HelloDBA.com ]Oracle IO问题解析
  • 原文地址:https://www.cnblogs.com/wxjor/p/6958661.html
Copyright © 2011-2022 走看看