当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

1092:Power Hungry Cows

题目描述
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.
输入解释
A single line with one integer: P.
输出解释
A single line with a single integer that is the minimum number of operations it requires to compute the power.
输入样例
31
输出样例
6

该题目包含在题集 ACM赛制

题目来源 USACO 2002 February

共提交 129

通过率 77.52%
时间上限 内存上限
1000 MS 30 MB