Description

Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = xxx,     x3 = x2xx,     x4 = x3xx,     ...  ,     x31 = x30xx.

The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute  x31 with eight multiplications:

x2 = xxx,     x3 = x2xx,     x6 = x3xx3,     x7 = x6xx,     x14 = x7xx7,
x15 = x14xx,    
x30 = x15xx15,    
x31 = x30xx.

This is not the shortest sequence of multiplications to compute 
x31. There are many ways with only seven multiplications. The following is one of them:

x2 = xxx,    
x4 = x2xx2,    
x8 = x4xx4,    
x10 = x8xx2,

x20 = x10xx10,    
x30 = x20xx10,    
x31 = x30xx.

There however is no way to compute 
x31 with fewer multiplications. Thus this is one of the most efficient ways to compute 
x31 only by multiplications.

If division is also available, we can find a shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):

x2 = xxx,    
x4 = x2xx2,    
x8 = x4xx4,    
x16 = x8xx8,    
x32 = x16xx16,    
x31 = x32 ÷ x.

This is one of the most efficient ways to compute 
x31 if a division is as fast as a multiplication.

Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence of operations should be x to a positive integer's power. In other words, x-3, for example, should never appear.

Input

The input is a sequence of one or more lines each containing a single integer n. n is positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output

Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.

Sample Input

1
31
70
91
473
512
811
953
0

Sample Output

0
6
8
9
11
9
13
12

解题思路:

题目大意:求用一个x,如何在最小的步数使用已经用过的凑出x^n,其中n是1~1000的,可以搜索,很容易想到,<=0和>=2000的点应该直接剪掉。

这个题目方根就是搜索的时候控制搜索的层数即可。

if(a[c]*(1<<(t-c))<n)
        return false;    //这个约束条件必须加,剪枝,这个点按最大比例每次都乘以2扩充也扩充不到n,那么就剪掉。

程序代码:

#include <iostream>
using namespace std;
int a[];
int t,b,n;
bool funt(int c)
{
if(c>t)
return false;
if(a[c]==n)
return true;
if(a[c]<=||a[c]>)
return false;
if(a[c]*(<<(t-c))<n)
return false;
for(int i=;i<=c;i++)
{
a[c+]=a[c]+a[i];
if(funt(c+))
return true;
a[c+]=a[c]-a[i];
if(funt(c+))
return true;
}
return ;
} int main()
{
while(cin>>n&&n)
{
if(n==)
cout<<<<endl;
else
{
t=;
while()
{
a[]=;
if(funt())
break;
t++;
}
cout<<t<<endl;
}
}
return ; }

暴力求解——POJ 3134Power Calculus的更多相关文章

  1. 暴力求解——POJ 1321 棋盘问题

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  2. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  3. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

  4. 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型

    先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...

  5. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  6. hdu6570Wave (暴力求解)

    Problem Description Avin is studying series. A series is called "wave" if the following co ...

  7. <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?

    str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n)  ,  暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...

  8. POJ 3175 Finding Bovine Roots (暴力求解)

    题意:给定一串数字,问你这是一个数字开方根得到的前几位,问你是哪个数字.析:如果 x.123... 这个数字的平方是一个整数的话,那必然sqr(x.124) > ceil(sqr(x.123)) ...

  9. POJ 3174 Alignment of the Planets (暴力求解)

    题意:给定 n 个坐标,问你三个共线的有多少组. 析:这个题真是坑啊,写着 n <= 770,那么一秒时间,三个循环肯定超时啊,我一直不敢写了,换了好几种方法都WA了,也不知道为什么,在比赛时坑 ...

随机推荐

  1. JAva Collections类方法详解

    http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...

  2. Android之如何混淆代码和相关配置

    昨天,客户想看一下目前项目开发到什么程度了,于是需要将项目签名打包成apk,结果打包的时候出错了,吃惊,什么情况.等成功打包以后,安装起来发现部分功能又报错了,囧,所幸最后还是解决了.在这里记录一下遇 ...

  3. SGU 177.Square(矩阵分割)

    时间限制:1.25s 空间限制:6M 题意: 给出n*n的矩阵(n<=1000),和m次涂色(m<=5000),每次涂色将一个子矩阵涂成白色或黑色,后涂的颜色将覆盖掉前面的颜色.初始所有格 ...

  4. IoC模式(控制反转)(转)

    转自:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html,写的很好,用C#代码解释控制反转,然后更进一步,提到依赖注入是控制反转的 ...

  5. 页面加载完成,但ie进度条一直加载

    页面ajax执行完删除等操作,会刷新当前页面,如果前端框架是左右iframe格式 我的前端页面提示用asyncBox,可能iframe和asyncBox的影响,出现这种情况: 网上大多数的说法是 页面 ...

  6. 如何使一个input文本框随其中内容而变化长度。

    第一:<input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(th ...

  7. 我的css reset

    @charset "utf-8"; /*reset*/ body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,f ...

  8. WordPress批量修改文章内容、URL链接、文章摘要

    通过SQL语句来批量修改wordpress博客内容,文章中所有语句都使用默认的wp_表前缀,如果您的数据表前缀不是wp_则需要在语句中作相应更改. 方法/步骤   批量修改文章内容 如果您想替换之前写 ...

  9. 访问Github过慢解决

    在这个地址查找响应最快的地址:http://tool.chinaz.com/dns?type=1&host=assets-cdn.github.com&ip= 查找:assets-cd ...

  10. 新站如何做SEO及注意事项

    最近公司做了新网站,完成后运营优化的工作就落在我身上了,由于之前也没有.就去网上百度了一下,上了各种论坛查阅大牛的博客.自己也总结了一些要点,在这里和大家分享一下.新网站大家可以点击查看牛羊养殖在线. ...