码迷,mamicode.com
首页 > 其他好文 > 详细

uva 1374 快速幂计算

时间:2015-02-17 23:29:52      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
#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\n",maxd);
                break;
            }
        }
    }
    return 0;
}
View Code

 

uva 1374 快速幂计算

标签:

原文地址:http://www.cnblogs.com/hanbinggan/p/4295511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!