zoukankan      html  css  js  c++  java
  • POJ 3134

    题意:求仅仅用乘法和除法最快多少步能够求到x^n

    思路:迭代加深搜索

    //Accepted	164K	1094MS	C++	840B
    include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int step[100005];
    int n;
    int cur;
    bool IDDFS(int lim,int g)
    {
    
        if(cur>lim) return false;
        if(step[cur]==g) return true;
        if((step[cur]<<(lim-cur))<g) return false; //剪枝
        for(int i=0;i<=cur;i++)
        {
            cur++;
            step[cur]=step[i]+step[cur-1];
            if(step[cur]<=10000&&IDDFS(lim,g)) return true; //比1000^2大的时候就没意义了,剪枝
            step[cur]=step[cur-1]-step[i];
            if(step[cur]>0&&IDDFS(lim,g)) return true;
            cur--;
        }
        return false;
    }
    int main()
    {
        while(scanf("%d",&n),n)
        {
            int ans=-1;
            while(1)
            {
                cur=0;
                step[cur] = 1;
                ans++;
                if(IDDFS(ans,n)) break;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }


  • 相关阅读:
    3.springMVC参数绑定过程(页面向后台传参)
    2.springMVC入门程序
    1.理解springMVC的原理
    RTO
    DC Congestion Control
    docs for DC Network
    FCT和QCT
    下行TM
    上行TM
    调度与队列
  • 原文地址:https://www.cnblogs.com/llguanli/p/8315299.html
Copyright © 2011-2022 走看看