zoukankan      html  css  js  c++  java
  • uva 1374 快速幂计算

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <cstdlib>
    #include <stack>
    #include <cctype>
    #include <string>
    #include <malloc.h>
    #include <queue>
    #include <map>
    
    using namespace std;
    
    const int INF = 0xffffff;
    const double esp = 10e-8;
    const double Pi = 4 * atan(1.0);
    const int Maxn = 2000+10;
    const long long mod =  2147483647;
    const int dr[] = {1,0,-1,0,-1,1,-1,1};
    const int dc[] = {0,1,0,-1,1,-1,-1,1};
    typedef long long LL;
    
    LL gac(LL a,LL b){
        return b?gac(b,a%b):a;
    }
    
    int n,ans[Maxn],maxd;
    bool vis[Maxn];
    
    int dfs(int step,int s){
        ans[step] = s;
        if(step == maxd){
            if(vis[n])
                return 1;
            return 0;
        }
        if(ans[step] > 2000 || s * 1 << (maxd-step) < n){
            return 0;
        }
        for(int i = 0;i<= step;i++){
            int t = s + ans[i];
            if(!vis[t]){
                vis[t] = 1;
                if(dfs(step+1,t))
                    return 1;
                vis[t] = 0;
            }
            t = abs(s-ans[i]);
            if(t > 0 && !vis[t]){
                vis[t] = 1;
                if(dfs(step+1,t))
                    return 1;
                vis[t] = 0;
            }
        }
        return 0;
    }
    
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("inpt.txt","r",stdin);
    #endif
        while(~scanf("%d",&n) && n){
    
            for(maxd = 0;;maxd++){
                memset(vis,0,sizeof(vis));
                vis[1] = 1;
                if(dfs(0,1)){
                    printf("%d
    ",maxd);
                    break;
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    jquery 页面滚动到底部事件
    01上古天真论 [音频]
    pyjnius 通过包名获取其他应用程序的名称
    python3 获取当前网络子网ip
    堆排序、快速排序、归并排序总结
    Linux 进程
    链表(转载)
    15-C语言结构体(转载)
    IP地址的分类
    TCP/IP详解
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4295511.html
Copyright © 2011-2022 走看看