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

【POJ】3134 Power Calculus

时间:2016-03-23 19:46:33      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

  1 /* 3134 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <bitset>
 12 #include <algorithm>
 13 #include <cstdio>
 14 #include <cmath>
 15 #include <ctime>
 16 #include <cstring>
 17 #include <climits>
 18 #include <cctype>
 19 #include <cassert>
 20 #include <functional>
 21 #include <iterator>
 22 #include <iomanip>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,1024000")
 25 
 26 #define sti                set<int>
 27 #define stpii            set<pair<int, int> >
 28 #define mpii            map<int,int>
 29 #define vi                vector<int>
 30 #define pii                pair<int,int>
 31 #define vpii            vector<pair<int,int> >
 32 #define rep(i, a, n)     for (int i=a;i<n;++i)
 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 34 #define clr                clear
 35 #define pb                 push_back
 36 #define mp                 make_pair
 37 #define fir                first
 38 #define sec                second
 39 #define all(x)             (x).begin(),(x).end()
 40 #define SZ(x)             ((int)(x).size())
 41 #define lson            l, mid, rt<<1
 42 #define rson            mid+1, r, rt<<1|1
 43 #define INF                0x3f3f3f3f
 44 #define mset(a, val)    memset(a, (val), sizeof(a))
 45 
 46 const int maxn = 2048;
 47 int ans[maxn];
 48 int a[maxn];
 49 bool visit[maxn];
 50 
 51 void printA(int *a) {
 52     int sz = 1001;
 53     
 54     rep(i, 0, sz) {
 55         if (i == 0) {
 56             printf("int ans[] = {%d", a[i]);
 57         } else {
 58             printf(", %d", a[i]);
 59         }
 60     }
 61     printf("};\n");
 62 }
 63 
 64 bool dfs(int dep, int step, int n) {
 65     if (visit[n])
 66         return true;
 67     
 68     if (dep > step)
 69         return false;
 70     
 71     if (a[dep-1]<<(step-dep+1) < n)
 72         return false;
 73     
 74     if (abs(n-a[dep-1])<n && step-dep>=ans[abs(n-a[dep-1])])
 75         return true;
 76     
 77     int tmp;
 78     
 79     rep(i, 0, dep) {
 80         tmp = a[dep-1] + a[i];
 81         if (tmp<maxn && !visit[tmp]) {
 82             visit[tmp] = true;
 83             a[dep] = tmp;
 84             if (dfs(dep+1, step, n))
 85                 return true;
 86             visit[tmp] = false;
 87         }
 88         tmp = a[dep-1] - a[i];
 89         if (tmp>0 && !visit[tmp]) {
 90             visit[tmp] = true;
 91             a[dep] = tmp;
 92             if (dfs(dep+1, step, n))
 93                 return true;
 94             visit[tmp] = false;
 95         }
 96     }
 97     
 98     return false;
 99 }
100 
101 int solve(int n) {
102     memset(visit, false, sizeof(visit));
103     visit[1] = true;
104     visit[0] = true;
105     a[0] = 1;
106     rep(i, 1, n+1) {
107         if (dfs(1, i, n)) {
108             return i;
109         }
110     }
111     
112     return -1;
113 }
114 
115 void init() {
116     memset(ans, 0x3f, sizeof(ans));
117     ans[1] = 0;
118     rep(i, 2, 1001)
119         ans[i] = solve(i);
120     printA(ans);
121 }
122 
123 int main() {
124     ios::sync_with_stdio(false);
125     #ifndef ONLINE_JUDGE
126         freopen("data.in", "r", stdin);
127         freopen("data.out", "w", stdout);
128     #endif
129     
130     init();
131     
132     #ifndef ONLINE_JUDGE
133         printf("time = %d.\n", (int)clock());
134     #endif
135     
136     return 0;
137 }

(2)打表程序

技术分享
 1 /* 3134 */
 2 #include <iostream>
 3 #include <sstream>
 4 #include <string>
 5 #include <map>
 6 #include <queue>
 7 #include <set>
 8 #include <stack>
 9 #include <vector>
10 #include <deque>
11 #include <bitset>
12 #include <algorithm>
13 #include <cstdio>
14 #include <cmath>
15 #include <ctime>
16 #include <cstring>
17 #include <climits>
18 #include <cctype>
19 #include <cassert>
20 #include <functional>
21 #include <iterator>
22 #include <iomanip>
23 using namespace std;
24 //#pragma comment(linker,"/STACK:102400000,1024000")
25 
26 #define sti                set<int>
27 #define stpii            set<pair<int, int> >
28 #define mpii            map<int,int>
29 #define vi                vector<int>
30 #define pii                pair<int,int>
31 #define vpii            vector<pair<int,int> >
32 #define rep(i, a, n)     for (int i=a;i<n;++i)
33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
34 #define clr                clear
35 #define pb                 push_back
36 #define mp                 make_pair
37 #define fir                first
38 #define sec                second
39 #define all(x)             (x).begin(),(x).end()
40 #define SZ(x)             ((int)(x).size())
41 #define lson            l, mid, rt<<1
42 #define rson            mid+1, r, rt<<1|1
43 #define INF                0x3f3f3f3f
44 #define mset(a, val)    memset(a, (val), sizeof(a))
45 
46 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};
47 
48 int main() {
49     ios::sync_with_stdio(false);
50     #ifndef ONLINE_JUDGE
51         freopen("data.in", "r", stdin);
52         freopen("data.out", "w", stdout);
53     #endif
54 
55     int n;
56 
57     while (cin>>n && n) {
58         cout << ans[n] << endl;
59     }
60 
61     #ifndef ONLINE_JUDGE
62         printf("time = %d.\n", (int)clock());
63     #endif
64 
65     return 0;
66 }
View Code


4. 数据生成器

 1 import sys
 2 import string
 3 from random import randint, shuffle
 4 
 5     
 6 def GenData(fileName):
 7     with open(fileName, "w") as fout:
 8         t = 1
 9         # fout.write("%d\n" % (t))
10         # bound = (2**32) - 1
11         for tt in xrange(t):
12             L = range(1, 1001)
13             fout.write("\n".join(map(str, L)) + "\n0")
14             
15             
16 def MovData(srcFileName, desFileName):
17     with open(srcFileName, "r") as fin:
18         lines = fin.readlines()
19     with open(desFileName, "w") as fout:
20         fout.write("".join(lines))
21 
22         
23 def CompData():
24     print "comp"
25     srcFileName = "F:\Qt_prj\hdoj\data.out"
26     desFileName = "F:\workspace\cpp_hdoj\data.out"
27     srcLines = []
28     desLines = []
29     with open(srcFileName, "r") as fin:
30         srcLines = fin.readlines()
31     with open(desFileName, "r") as fin:
32         desLines = fin.readlines()
33     n = min(len(srcLines), len(desLines))-1
34     for i in xrange(n):
35         ans2 = int(desLines[i])
36         ans1 = int(srcLines[i])
37         if ans1 > ans2:
38             print "%d: wrong" % i
39 
40             
41 if __name__ == "__main__":
42     srcFileName = "F:\Qt_prj\hdoj\data.in"
43     desFileName = "F:\workspace\cpp_hdoj\data.in"
44     GenData(srcFileName)
45     MovData(srcFileName, desFileName)
46     

 

【POJ】3134 Power Calculus

标签:

原文地址:http://www.cnblogs.com/bombe1013/p/5312400.html

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