[USACO2002][poj1945]Power Hungry Cows(启发式搜索)

时间:2022-09-16 17:44:57

Power Hungry Cows
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4570 Accepted: 1120

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

Source

USACO 2002 February

分析:先bfs然后发现各种乱搞都不行……然后本渣去打表……悲剧的是最后的表太大了poj上交不去……然后找到了这个神一般的东西Orz http://www.cppblog.com/varg-vikernes/archive/2010/02/18/108024.aspx 额不过最后启发式搜索水过的……

[USACO2002][poj1945]Power Hungry Cows(启发式搜索)的更多相关文章

  1. 『Power Hungry Cows A&ast;启发式搜索』

    Power Hungry Cows(POJ 1945) Description FJ的奶牛想要快速计算整数P的幂 (1 <= P <=20,000),它们需要你的帮助.因为计算极大数的幂, ...

  2. 【BFS】Power Hungry Cows

    Power Hungry Cows Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5522   Accepted: 1384 ...

  3. poj 1945 Power Hungry Cows A&ast;

    Description:     就是给你一个数,你可以把它自乘,也可以把他乘或除以任意一个造出过的数,问你最多经过多少次操作能变换成目标数 思路:这题真的不怎么会啊.n = 20000,每一层都有很 ...

  4. BZOJ1669&colon; &lbrack;Usaco2006 Oct&rsqb;Hungry Cows饥饿的奶牛

    1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 665  Solved: 419 ...

  5. BZOJ 1669&colon; &lbrack;Usaco2006 Oct&rsqb;Hungry Cows饥饿的奶牛&lpar; LIS &rpar;

    裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...

  6. 【动态规划】bzoj1669 &lbrack;Usaco2006 Oct&rsqb;Hungry Cows饥饿的奶牛

    #include<cstdio> #include<algorithm> using namespace std; int n,a[5001],b[5001],en; int ...

  7. 【BZOJ】1669&colon; &lbrack;Usaco2006 Oct&rsqb;Hungry Cows饥饿的奶牛(lis)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1669 水题太严重 #include <cstdio> #include <cstr ...

  8. bzoj 1669&colon; &lbrack;Usaco2006 Oct&rsqb;Hungry Cows饥饿的奶牛【dp&plus;树状数组&plus;hash】

    最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...

  9. 【极大化剪枝】Power Hungry Cows-C&plus;&plus;【没有用A&ast;!】【超级简单!】

    Description小KITTY想要快速计算整数P的幂 (1 <= P <=10,000),它们需要你的帮助.因为计算极大数的幂,所以它们同一时间仅能使用2个存储器,每个存储器可记录某个 ...

随机推荐

  1. &lbrack;&period;net 面向对象程序设计进阶&rsqb; &lpar;6&rpar; Lamda表达式&lpar;二&rpar; 表达式树快速入门

    [.net 面向对象程序设计进阶] (6) Lamda表达式(二) 表达式树快速入门 本节导读: 认识表达式树(Expression Tree),学习使用Lambda创建表达式树,解析表达式树. 学习 ...

  2. C语言&lpar;函数&rpar;学习之index、rindex

    函数定义:char *index(const char *s, int c); 头文件:    #include strings.h 函数说明:index()用来找出参数s 字符串中第一个出现的参数c ...

  3. ubuntu安装ssh

    为了解决远程连接ubuntu服务器控制端,方便操作.ubuntu不同的版本安装方式一致!首先在ubuntu服务器下安装SSH服务linux安装命令:sudo apt-get install opens ...

  4. Ubuntu 12&period;04 禁用触摸板

    昨天把系统换为Backbox了,版本为Ubuntu12.04,装完后发现其触摸板不能禁用,之前在其他版本都是直接快捷键就可关闭或者启用触摸板,解决方法如下: sudo add-apt-reposito ...

  5. innodb b&plus;树

    http://www.admin10000.com/document/5372.html

  6. MySQL长短密码

    MySQL长短密码 今天批量搭建MySQL环境的时候,遇到长短密码问题,故就此问题总结一下长短密码. 介绍 1.长密码例子: mysql> show grants for 'test'@'loc ...

  7. &period;Net基础——程序集与CIL

    1. 程序集和CIL: 程序集是由.NET语言的编译器接受源代码文件产生的输出文件,通常分为 exe和dll两类,其中exe包含Main入口方法可以双击执行,dll则需要被其他程序集调用执行. CIL ...

  8. 全球第一款纯数据GPRS模块 有方M590 概述

    更多精彩请到http://blog.tuzhuke.info/?cat=30 M590为全球第一款纯数据GPRS模块,专注数据收发功能,GPRS数据以及短信数据.没有电话语音功能,可以能够拨打或者接听 ...

  9. andorid 帧布局

    framelayout.xml帧布局 <?xml version="1.0" encoding="utf-8"?> <FrameLayout ...

  10. &lpar;19&sol;24&rpar; webpack实战技巧:推荐使用的第三方类库打包方法

    在日常的开发中,总避免不了引入第三方的框架,比如常用的JQuery,此节我们来学习一下如何优雅并正确的用webpack引入第三方库. 这里我们以第三方框架JQuery为例: 1.在入口文件中引入 1. ...