浏览器标题切换
浏览器标题切换end

寒假Day47:Power Calculus-IDA*(dfs+剪枝)

题面:

复制代码
Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x.

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

x2 = x × x, x3 = x2 × x, x6 = x3 × x3, x7 = x6 × x, x14 = x7 × x7, x15 = x14 × x, x30 = x15 × x15, x31 = x30 × x.

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 = x × x, x4 = x2 × x2, x8 = x4 × x4, x8 = x4 × x4, x10 = x8 × x2, x20 = x10 × x10, x30 = x20 × x10, x31 = x30 × x.

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

x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x16 = x8 × x8, x32 = x16 × x16, 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 should be x to a positive integer’s power. In others 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.
View Code
复制代码

 

测试数据:

复制代码
Sample Input
1
31
70
91
473
512
811
953
0

Sample Output
0
6
8
9
11
9
13
12
复制代码

 

题意:给出一个n,计算x-> x^n (通过乘或除运算)的最小步数

 

剪枝条件:目前枚举到的步数+最坏情况还到不了n,就可以剪枝了

剪枝部分看代码吧

 

不会的话可以根据代码手动模拟一下。

 

涉及算法:IDA*

IDA*算法就是基于迭代加深的A*算法。

A*算法是一种静态路网中求解最短路最有效的直接搜索方法。

(其实我感觉就是一个dfs+剪枝)

 

复制代码
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;

int n,ans;
int a[35];

bool dfs(int x, int y)
{
    int w=y<<(ans-x);    
    if(x>ans||y<=0||w<n)//当当前数y在最坏情况下<n,说明到不了x^n
        return 0;
    if(y==n||w==n)
        return 1;
    a[x]=y;
    for(int i=0; i<=x; i++)
    {
        if(dfs(x+1,y+a[i])||dfs(x+1,y-a[i]))
            return 1;
    }
    return 0;
}

int main()
{
    while(~scanf("%d",&n)&&n)//x-> x^n
    {
        for(int i=0; ; i++)//枚举次数,从0开始
        {
            ans=i;
            if(dfs(0,1))//说明x==(x^n)了
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}
复制代码

 

 

 

 

posted @   抓水母的派大星  阅读(155)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示