poj 1945 Power Hungry Cows(BFS)(简单题)

Power Hungry Cows
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4821 Accepted: 1180

Description

FJ's cows would like to be able to compute integer powers P (1 <= P <= 20,000) of numbers very quickly, but need your help. Because they're going to be computing powers of very large numbers, they can only keep around two work variables for intermediate results. 

The first of those work variables is initialized to the number (denoted x) for which they are calculating the power; the other is initialized to 1. The cows can both multiply and divide any pair of the work variables and store the result in any work variable, but all results are stored as integers. 

For example, if they want to compute x^31, one way to perform the calculation is: 
                                              WV1  WV2

                                      Start:   x    1

   Multiply first by first, store in second:   x   x^2

                  Multiply second by second:   x   x^4

                  Multiply second by second:   x   x^8

                  Multiply second by second:   x   x^16

                  Multiply second by second:   x   x^32

                     Divide second by first:   x   x^31

Thus, x^31 can computed in six operations. Given the power to be computed and the the number of work variables, find the minimum number of operations to calculate the power. 

Input

A single line with one integer: P. 

Output

A single line with a single integer that is the minimum number of operations it requires to compute the power. 

Sample Input

31

Sample Output

6

思路:

很简单的题目,直接BFS。由(0,1)经过变换到目标所需最先步数。

先放代码上来,有空再改改

代码:

//9412K	172MS
#include <stdio.h>
#include<queue>

const int maxn = 20003, maxq = 700003, prime0 = 20101, prime1 = 97;
int n, n2, answer, head, last, q[maxq][3];
bool hash[prime0][prime1];

bool add_node(int wv0,int wv1,int step)
{
    if (wv0 == n || wv1 == n)
        return true;
    if (wv0 < wv1)
    {
        int temp = wv0;
        wv0 = wv1;
        wv1 = temp;
    }
    if (wv0 == wv1 || wv0 >= n2 || wv1 >= prime1)
        return false;
    if (!hash[wv0][wv1])
    {
        hash[wv0][wv1] = true;
        ++last;
        q[last][0] = wv0;
        q[last][1] = wv1;
        q[last][2] = step;
    }
    return false;
}

int bfs() {
    n2 = n + prime1;
    head = last = -1;
    add_node(1, 0, 0);
    while(head < last)
    {
        ++head;
        int wv0 = q[head][0], wv1 = q[head][1], step = q[head][2] + 1;
        if(add_node(wv0+wv0, wv1, step) ||
           add_node(wv0+wv1, wv1, step) ||
           add_node(wv1+wv1, wv1, step) ||
           add_node(wv0, wv0+wv0, step) ||
           add_node(wv0, wv0+wv1, step) ||
           add_node(wv0, wv1+wv1, step) ||
           add_node(wv0, wv0-wv1, step) ||
           add_node(wv0-wv1, wv0, step))
           {
                answer = step;
                break;
           }
    }
    return 0;
}

int main()
{
    scanf("%d", &n);
    bfs();
    printf("%d\n", answer);
    return 0;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值