标签:
[2016-03-03][UVA][1374][Power Calculus]#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; typedef long long LL; #define CLR(x,y) memset((x),(y),sizeof((x))) #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x)) #define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x)) #define FOR2(x,y,z) for((x)=(y);(x)<(z);++(x)) #define FORD2(x,y,z) for((x)=(y);(x)>=(z);--(x)) const int maxn = 1000 + 10; int n,arr[maxn * 2];//arr保存出现过的指数 int dfs(int cur,int curd,const int & maxd){ if (curd == maxd) return arr[cur - 1] == n; //当前值最多可以平方(1<<(maxd - curd))次,计算这么多次数之后,是否小于n if (arr[cur-1] * 1<<(maxd - curd) < n) return 0; //枚举出现过的指数 FORD(i,cur - 1,0){ //产生新值,进入下一层 arr[cur] = arr[cur-1] + arr[i]; if (dfs(cur + 1,curd + 1, maxd)) return 1; arr[cur] = arr[cur-1] - arr[i]; if (dfs(cur + 1,curd + 1, maxd)) return 1; } return 0; } int init(){ int tmp = n,cnt = 0; while(tmp > 1){ tmp /= 2; ++cnt; } return cnt; } int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while (~scanf("%d",&n) && n){ for(int i = init();; ++i){ arr[0] = 1; if(dfs(1,0,i)){ printf("%d\n",i);break; } } } return 0; } |
[2016-03-03][UVA][1374][Power Calculus]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/5239685.html