1. 题目描述
给定一个正整数$n$,求经过多少次乘法或除法运算可以从$x$得到$x^n$?中间结果也是可以复用的。

2. 基本思路
实际结果其实非常小,肯定不会超过20。因此,可以采用IDA*算法。
注意几个剪枝优化就好了:
(1)每次新计算的值必须从未出现过;
(2)每次新计算的值进行还可以执行的运算次数的幂运算仍然小于$x^n$,即新值左移还可以执行的次数小于$n$则一定不成立;
(3)该值与$n$的绝对值$\Delta$小于$n$,同时还可以执行的次数大于$ans[\Delta]+1$,那么一定成立。

3. 代码
(1)生成ans数组程序

 /* 3134 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define INF 0x3f3f3f3f
#define mset(a, val) memset(a, (val), sizeof(a)) const int maxn = ;
int ans[maxn];
int a[maxn];
bool visit[maxn]; void printA(int *a) {
int sz = ; rep(i, , sz) {
if (i == ) {
printf("int ans[] = {%d", a[i]);
} else {
printf(", %d", a[i]);
}
}
printf("};\n");
} bool dfs(int dep, int step, int n) {
if (visit[n])
return true; if (dep > step)
return false; if (a[dep-]<<(step-dep+) < n)
return false; if (abs(n-a[dep-])<n && step-dep>=ans[abs(n-a[dep-])])
return true; int tmp; rep(i, , dep) {
tmp = a[dep-] + a[i];
if (tmp<maxn && !visit[tmp]) {
visit[tmp] = true;
a[dep] = tmp;
if (dfs(dep+, step, n))
return true;
visit[tmp] = false;
}
tmp = a[dep-] - a[i];
if (tmp> && !visit[tmp]) {
visit[tmp] = true;
a[dep] = tmp;
if (dfs(dep+, step, n))
return true;
visit[tmp] = false;
}
} return false;
} int solve(int n) {
memset(visit, false, sizeof(visit));
visit[] = true;
visit[] = true;
a[] = ;
rep(i, , n+) {
if (dfs(, i, n)) {
return i;
}
} return -;
} void init() {
memset(ans, 0x3f, sizeof(ans));
ans[] = ;
rep(i, , )
ans[i] = solve(i);
printA(ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif init(); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

(2)打表程序

 /* 3134 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define INF 0x3f3f3f3f
#define mset(a, val) memset(a, (val), sizeof(a)) int ans[] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , }; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n; while (cin>>n && n) {
cout << ans[n] << endl;
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

4. 数据生成器

 import sys
import string
from random import randint, shuffle def GenData(fileName):
with open(fileName, "w") as fout:
t = 1
# fout.write("%d\n" % (t))
# bound = (2**32) - 1
for tt in xrange(t):
L = range(1, 1001)
fout.write("\n".join(map(str, L)) + "\n0") def MovData(srcFileName, desFileName):
with open(srcFileName, "r") as fin:
lines = fin.readlines()
with open(desFileName, "w") as fout:
fout.write("".join(lines)) def CompData():
print "comp"
srcFileName = "F:\Qt_prj\hdoj\data.out"
desFileName = "F:\workspace\cpp_hdoj\data.out"
srcLines = []
desLines = []
with open(srcFileName, "r") as fin:
srcLines = fin.readlines()
with open(desFileName, "r") as fin:
desLines = fin.readlines()
n = min(len(srcLines), len(desLines))-1
for i in xrange(n):
ans2 = int(desLines[i])
ans1 = int(srcLines[i])
if ans1 > ans2:
print "%d: wrong" % i if __name__ == "__main__":
srcFileName = "F:\Qt_prj\hdoj\data.in"
desFileName = "F:\workspace\cpp_hdoj\data.in"
GenData(srcFileName)
MovData(srcFileName, desFileName)

【POJ】3134 Power Calculus的更多相关文章

  1. 【POJ】2406 Power Strings

    http://poj.org/problem?id=2406 题意:给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值.(长度<=1000000) #i ...

  2. 【UVa】1374 Power Calculus(IDA*)

    题目 题目     分析 IDA*大法好,抄了lrj代码.     代码 #include <cstdio> #include <cstring> #include <a ...

  3. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  4. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  5. 【POJ】3233 Matrix Power Series

    [算法]二分+矩阵快速幂 [题意]给定矩阵A和整数k,MOD,求A^0+A^1+A^2+...+A^k. [题解] 定义题目要求的答案为f(n),即: $$f_n=\sum_{i=0}^{n}A^i$ ...

  6. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  7. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  8. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  9. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

随机推荐

  1. intellij 设置-试验过的

    1.已修改的文件星号“*”标记 2.在PROJECT窗口中快速定位,编辑窗口中的文件 在编辑的所选文件按ALT+F1, 然后选择PROJECT VIEW 3.改变编辑文本字体大小 FILE -> ...

  2. 简单的C#线程开发实例(隔一秒改变一下Label的Text)

    要实现的效果:点击按纽,窗口上的label上出现1~100数字的变化. 第一个实例(把窗口上的label上文字改成0): using System; using System.Windows.Form ...

  3. hadoop相关问题

    发现一篇不错的文章,转一下.http://www.cnblogs.com/xuekyo/p/3386610.html HDFS导论(转)   1.流式数据访问 HDFS的构建思想是这样的:一次写入,多 ...

  4. SQL_SERVER_2008升级SQL_SERVER_2008_R2办法 (一、升级;二、重新xie载安装)

    SQL_SERVER_2008升级SQL_SERVER_2008_R2两种办法   今天将由于需要就将我的SQL 2008升级到SQL 2008 R2. 说到为什么要升级是因为,因附加数据库时发现报错 ...

  5. UML中的六种关系的比较与学习

    通过不断的学习并绘制UML图,整个画图的过程中深刻体会到其核心部分还是理解事物之间的关系,总结六大关系来深入学习,主要关系有六种:继承.实现.依赖.关联.聚合.组合. 区别于联系:         1 ...

  6. 下拉刷新ListView实现原理

    (1)主要是onScroll()方法和onTouchEvent()方法,先是onTouchEvent()的ACTION_DOWN,然后是 ACTION_MOVE和onScroll()方法同时进行,最后 ...

  7. 《Dive into Python》Chapter 2 and Chapter 3 笔记

    Example 2.1. odbchelper.py def buildConnectionString(params): """Build a connection s ...

  8. [设计模式] 10 外观模式 facade

    外观模式应该是用的很多的一种模式,特别是当一个系统很复杂时,系统提供给客户的是一个简单的对外接口,而把里面复杂的结构都封装了起来.客户只需使用这些简单接口就能使用这个系统,而不需要关注内部复杂的结构. ...

  9. Filter及FilterChain的使用详解(转)

    一.Filter的介绍及使用 什么是过滤器? 与Servlet相似,过滤器是一些web应用程序组件,可以绑定到一个web应用程序中.但是与其他web应用程序组件不同的是,过滤器是"链&quo ...

  10. uva 10887

    是个 hash  用的容器类水过 #include <iostream> #include <cstdio> #include <string> #include ...