解题思路:

  这是一道以快速幂计算为原理的题,实际上也属于求最短路径的题目类型。那么我们可以以当前求出的幂的集合为状态,采用IDA*方法即可求解。问题的关键在于如何剪枝效率更高。笔者采用的剪枝方法是:

  1)如果当前状态幂集合中的最大元素max满足 max*2^(maxd-cur_d)<n,则剪枝。原因是:在每一次状态转移后,max最多增大一倍。(maxd-cur_d)次转移之后,max最多变成原来的2^(maxd-cur_d)倍,然而如果当前状态的极限情况下仍有max<n,则当前状态结点一定无法出解。

  2)解的幂集合中最多只有一个元素比目标n大。

采用反证法,证明如下:

  假设解的幂集合中存在m2>m1>n ,那么必然存在从m2到达m1的路径p1,和从m1到达n的路径p2(否则与假设矛盾)。

  设路径p1花费步数s1,路径p2花费步数s2,那么从m2到达n的步数为s3=s2+s1>s2。

  然而由于我们采用IDA*算法,由于s2<s3,路径p2会先被找到,当前状态不会出现,产生矛盾,得证。

有了以上优化思路,代码效率将会极大提高。

代码如下:

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <set>
#include <algorithm> using namespace std;
#define time__ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC) int tar_n;
int power[+];
vector<int> S;
int maxd;
int S_upper_n(){
int cnt=;
for(int i=;i<S.size();i++)
if(S[i]>tar_n) cnt++;
return cnt;
}
bool dfs(int d){
if(d==maxd){
if(power[tar_n]) return true;
else return false;
}
int S_max=;
for(int i=;i<S.size();i++)
S_max=max(S_max,S[i]);
if(((S_max)<<(maxd-d))<tar_n||S_upper_n()>)
return false;
for(int i=S.size()-;i>=;i--){
int t;
t=S[S.size()-]+S[i];
if(power[t]==){
S.push_back(t);
power[t]=;
if(dfs(d+)) return true;
S.pop_back();
power[t]=;
}
t=S[S.size()-]-S[i];
if(t>&&power[t]==){
S.push_back(t);
power[t]=;
if(dfs(d+)) return true;
S.pop_back();
power[t]=;
}
}
return false;
}
bool solve(){
memset(power, , sizeof power);
S.clear();
S.push_back();
power[]=;
if(dfs())
return true;
return false;
}
int main() {
while(scanf("%d",&tar_n)&&tar_n){
maxd=;
int temp=;
while(temp<tar_n){
maxd++;
temp+=temp;
}
for(;;maxd++)
if(solve()){
printf("%d\n",maxd);
//time__;
break;
}
}
//time__;
return ;
}

UVa 1374 - Power Calculus——[迭代加深搜索、快速幂]的更多相关文章

  1. UVA - 1374 Power Calculus (dfs迭代加深搜索)

    题目: 输入正整数n(1≤n≤1000),问最少需要几次乘除法可以从x得到xn ?在计算过程中x的指数应当总是正整数. 思路: dfs枚举次数深搜 注意: 1.指数如果小于0,就退出当前的搜索 2.n ...

  2. UVA-1374 Power Calculus (迭代加深搜索)

    题目大意:问最少经过几次乘除法可以使x变成xn. 题目分析:迭代加深搜索. 代码如下: # include<iostream> # include<cstdio> # incl ...

  3. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  4. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

  5. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  6. UVA 11212 Editing a Book [迭代加深搜索IDA*]

    11212 Editing a Book You have n equal-length paragraphs numbered 1 to n. Now you want to arrange the ...

  7. poj 3134 Power Calculus(迭代加深dfs+强剪枝)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  8. UVA1374-Power Calculus(迭代加深搜索)

    Problem UVA1374-Power Calculus Accept:323  Submit:2083 Time Limit: 3000 mSec  Problem Description  I ...

  9. UVA 1374 Power Calculus

    题意: 给出m,问对n最少进行几次操作.n初始为1,能得到m.操作1位将n平方.操作2为将n除以之前出现的n值中的任意一个. 分析: 其实是关于指数的操作,即从1到m最少的步数.我们可以先确定最少步数 ...

随机推荐

  1. SQL注入绕过的技巧总结

    sql注入在很早很早以前是很常见的一个漏洞.后来随着安全水平的提高,sql注入已经很少能够看到了.但是就在今天,还有很多网站带着sql注入漏洞在运行.稍微有点安全意识的朋友就应该懂得要做一下sql注入 ...

  2. Contentprovider 注册 启动简单流程

    安装app时packagemanager 读取manixfest获取provider信息 存在数据库里流程:1.加载ActivityThread main方法,创建消息队列.ActivityThrea ...

  3. 数据ETL是指什么

    ETL是数据抽取(Extract).清洗(Cleaning).转换(Transform).装载(Load)的过程.是构建数据仓库的重要一环,用户从数据源抽取出所需的数据,经过数据清洗,最终按照预先定义 ...

  4. 删除指定节点Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  5. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  6. 【JZOJ1922】【Usaco 2005 NOV Gold】小行星群

    题目描述 Bessie想驾驶她的飞船穿过危险的小行星群,小行星群是一个N×N的网格(1 <= N <= 500),在网格内有K个小行星(1 <= K <= 10,000). 幸 ...

  7. poj2112 最大流

    我用Dinic写的.G++ 1800ms 很慢,c++直接超时.优化后的 141ms,很快! 对于此题,建图方法很巧妙,通常想到求距离,那就会朝距离的方向建图,但是这题根据牛个数来建图,然后二分距离. ...

  8. hdu1564 简单博弈

    多画几个图可以发现规律: #include<stdio.h> int main() { int i,n; while(scanf("%d",&n)!=EOF) ...

  9. MaxCompute 费用暴涨之存储压缩率降低导致SQL输入量变大

    现象:同样的SQL,每天处理的数据行数差不多,但是费用突然暴涨甚至会翻数倍. 分析: 我们先明确MaxCompute SQL后付费的计费公式:一条SQL执行的费用=扫描输入量 ️ SQL复杂度 ️ 0 ...

  10. 冒泡排序&&选择排序 以及时间效率对比

    package com.test4; import java.util.*; //Calendar 显示时间 /** * @author qingfeng * 功能:冒泡排序 */ public cl ...