【POJ】3134 Power Calculus
2016-03-23 18:43
295 查看
1. 题目描述
给定一个正整数$n$,求经过多少次乘法或除法运算可以从$x$得到$x^n$?中间结果也是可以复用的。
2. 基本思路
实际结果其实非常小,肯定不会超过20。因此,可以采用IDA*算法。
注意几个剪枝优化就好了:
(1)每次新计算的值必须从未出现过;
(2)每次新计算的值进行还可以执行的运算次数的幂运算仍然小于$x^n$,即新值左移还可以执行的次数小于$n$则一定不成立;
(3)该值与$n$的绝对值$\Delta$小于$n$,同时还可以执行的次数大于$ans[\Delta]+1$,那么一定成立。
3. 代码
(1)生成ans数组程序
(2)打表程序
View Code
4. 数据生成器
给定一个正整数$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 = 2048; int ans[maxn]; int a[maxn]; bool visit[maxn]; void printA(int *a) { int sz = 1001; rep(i, 0, sz) { if (i == 0) { printf("int ans[] = {%d", a[i]); } else { printf(", %d", a[i]); } } printf("};\n"); } bool dfs(int dep, int step, int n) { if (visit ) return true; if (dep > step) return false; if (a[dep-1]<<(step-dep+1) < n) return false; if (abs(n-a[dep-1])<n && step-dep>=ans[abs(n-a[dep-1])]) return true; int tmp; rep(i, 0, dep) { tmp = a[dep-1] + a[i]; if (tmp<maxn && !visit[tmp]) { visit[tmp] = true; a[dep] = tmp; if (dfs(dep+1, step, n)) return true; visit[tmp] = false; } tmp = a[dep-1] - a[i]; if (tmp>0 && !visit[tmp]) { visit[tmp] = true; a[dep] = tmp; if (dfs(dep+1, step, n)) return true; visit[tmp] = false; } } return false; } int solve(int n) { memset(visit, false, sizeof(visit)); visit[1] = true; visit[0] = true; a[0] = 1; rep(i, 1, n+1) { if (dfs(1, i, n)) { return i; } } return -1; } void init() { memset(ans, 0x3f, sizeof(ans)); ans[1] = 0; rep(i, 2, 1001) 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 0; }
(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[] = {1061109567, 0, 1, 2, 2, 3, 3, 4, 3, 4, 4, 5, 4, 5, 5, 5, 4, 5, 5, 6, 5, 6, 6, 6, 5, 6, 6, 6, 6, 7, 6, 6, 5, 6, 6, 7, 6, 7, 7, 7, 6, 7, 7, 7, 7, 7, 7, 7, 6, 7, 7, 7, 7, 8, 7, 8, 7, 8, 8, 8, 7, 8, 7, 7, 6, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 8, 7, 8, 8, 8, 8, 8, 8, 9, 8, 9, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 8, 9, 8, 9, 8, 9, 9, 9, 8, 9, 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9, 9, 8, 9, 8, 8, 7, 8, 8, 9, 8, 9, 9, 9, 8, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9, 9, 9, 9, 9, 10, 9, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9, 9, 9, 9, 9, 10, 9, 10, 9, 10, 9, 10, 10, 10, 9, 10, 10, 10, 9, 10, 10, 10, 9, 10, 9, 10, 9, 9, 9, 9, 8, 9, 9, 9, 9, 10, 9, 10, 9, 10, 10, 10, 9, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 9, 10, 9, 9, 8, 9, 9, 10, 9, 10, 10, 10, 9, 10, 10, 11, 10, 11, 10, 10, 9, 10, 10, 11, 10, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 11, 10, 10, 10, 11, 10, 11, 11, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 10, 10, 10, 10, 10, 9, 10, 10, 10, 10, 10, 10, 11, 10, 11, 10, 11, 10, 11, 11, 11, 10, 11, 11, 11, 10, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 10, 11, 11, 11, 10, 11, 10, 11, 10, 10, 10, 10, 9, 10, 10, 10, 10, 11, 10, 11, 10, 11, 11, 11, 10, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 11, 12, 11, 11, 11, 12, 11, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 11, 12, 11, 11, 10, 11, 11, 12, 11, 12, 11, 11, 10, 11, 11, 11, 10, 11, 10, 10, 9, 10, 10, 11, 10, 11, 11, 11, 10, 11, 11, 12, 11, 12, 11, 11, 10, 11, 11, 12, 11, 12, 12, 11, 11, 12, 12, 12, 11, 12, 11, 11, 10, 11, 11, 12, 11, 12, 12, 12, 11, 11, 12, 12, 11, 12, 11, 12, 11, 11, 11, 12, 11, 12, 11, 11, 11, 12, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 12, 11, 12, 12, 12, 11, 12, 11, 12, 11, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 11, 12, 11, 12, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 11, 11, 11, 12, 11, 12, 11, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 11, 12, 11, 11, 11, 11, 10, 11, 11, 11, 11, 12, 11, 12, 11, 12, 12, 12, 11, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 12, 12, 12, 12, 11, 12, 12, 12, 12, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 12, 12, 12, 13, 12, 13, 12, 12, 12, 13, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 13, 12, 12, 12, 13, 12, 13, 12, 12, 12, 13, 12, 13, 12, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 13, 13, 12, 13, 13, 13, 12, 13, 12, 13, 12, 13, 13, 13, 12, 13, 13, 13, 12, 13, 12, 13, 12, 12, 12, 13, 12, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 12, 13, 12, 12, 12, 12, 12, 13, 12, 13, 13, 13, 12, 13, 13, 13, 12, 13, 12, 12, 11, 12, 12, 13, 12, 13, 13, 13, 12}; 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 << endl; } #ifndef ONLINE_JUDGE printf("time = %d.\n", (int)clock()); #endif return 0; }
View Code
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)
相关文章推荐
- spring-mvc中返回fastjson的json数据去掉null值
- webapp和mobileapp分析
- C++设计模式之工厂方法模式(Factory Method)
- java并发包CyclicBarrier
- INSTALL_PARSE_FAIL_NO_CERTIFICATE 打jar包的时候遇到的问题。
- Java核心知识点-一些基本概念
- hdu5025(bfs,状态压缩)
- FragmentTransaction replace add
- think python学习心得-(3)条件和递归
- 作业管理系统数据字典
- Codeforces7C 扩展欧几里得
- 3.5.Android控件架构与自定义控件详解之自定义View(四)
- 深度优先搜索
- Merge Two Sorted Lists
- github开源Android资源整理(十三)大神个人和组织
- Android Manager
- 行测材料分析公式总结
- C++学习(一)——安装gcc编译环境
- android:layout_weight属性的工作原理
- 第四周,项目三,2,小学生算术能力测试系统