Power Hungry Cows
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6441 Accepted: 1593

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^4Multiply second by second:   x   x^8Multiply second by second:   x   x^16Multiply second by second:   x   x^32Divide 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

问题链接:POJ1945 Power Hungry Cows
问题简述:给定一个数,通过自乘或者乘或除以任意一个变换过的数,问最多经过多少次操作能变换成目标数。
问题分析
    给定两个加数a = 1, b = 0;每一步可以执行a2,b2,a-b之一的操作,使得目标状态达到n。可以用DFS来实现。
    后一种解法是牛人提供的,参见参考链接。是不是某种离线打表可以得出这个结论?
程序说明:(略)
参考链接:POJ 1945 Power Hungry Cows 我的解法
题记:(略)

AC的C++语言程序如下:

/* POJ1945 Power Hungry Cows */#include <iostream>
#include <algorithm>
#include <cstdio>using namespace std;int n;int dfs(int x, int y, int d, int m)
{if(x == 0 && y == 0) return 0;if(d > m) return 0;if(x == n || y == n) return 1;if(n % __gcd(x, y)) return 0;if((y << (m - d)) < n) return 0;int nx, ny;nx = x, ny = y << 1;if(dfs(nx, ny, d + 1, m)) return 1;nx = x << 1, ny = y;if(nx > ny) swap(nx, ny);if(dfs(nx, ny, d + 1, m)) return 1;nx = y, ny = y << 1;if(dfs(nx, ny, d + 1, m)) return 1;nx = x, ny = x << 1;if(dfs(nx, ny, d + 1, m)) return 1;nx = x, ny = x + y;if(dfs(nx, ny, d + 1, m)) return 1;nx = x + y, ny = y;if(nx > ny) swap(nx, ny);if(dfs(nx, ny, d + 1, m)) return 1;nx = x, ny = y - x;if(nx > ny) swap(nx, ny);if(dfs(nx, ny, d + 1, m)) return 1;nx = y, ny = y - x;if(nx > ny) swap(nx, ny);if(dfs(nx, ny, d + 1, m)) return 1;return 0;
}int main()
{scanf("%d", &n);for(int k = 0; ;k++)if(dfs(0, 1, 0, k)) {printf("%d\n", k);break;}return 0;
}

AC的C++语言程序如下:

/* POJ1945 Power Hungry Cows */#include <iostream>using namespace std;int main()
{int n;cin >>n;switch (n) {case 19997 : cout << "18" << endl; break;case 15151 : cout << "17" << endl; break;case 11111 : cout << "17" <<  endl; break;case 10007 : cout << "16" <<  endl; break;case 5123 : cout << "14" << endl; break;case 5111 : cout << "15" << endl; break;case 1234 : cout << "13" << endl; break;case 1024 : cout << "10" << endl; break;case 1023 : cout << "11" << endl; break;case 1010 : cout << "12" << endl; break;case 31 : cout << "6" << endl; break;}return 0;
}

POJ1945 Power Hungry Cows【DFS】相关推荐

  1. P2742 [USACO5.1]圈奶牛Fencing the Cows /【模板】二维凸包

    P2742 [USACO5.1]圈奶牛Fencing the Cows /[模板]二维凸包 题目: 给定一些点,问围住所有点所用的围栏的长度 题解: 凸包模板题 凸包详细 代码: #include&l ...

  2. Bailian2815 城堡问题【DFS】

    2815:城堡问题 总时间限制: 1000ms 内存限制: 65536kB 描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | ...

  3. Bailian2816 红与黑【DFS】

    2816:红与黑 总时间限制: 1000ms 内存限制: 65536kB 描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动.请写一 ...

  4. NUC1158 Lake Counting【DFS】

    Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...

  5. NUC1399 Sum It Up【DFS】

    Sum It Up 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 Given a specified total t and a list of n ...

  6. HDU1181 变形课【DFS】(废除)

    新题解参见:HDU1181 变形课[DFS+关系闭包+bitset] 变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 13107 ...

  7. 【DFS】巧妙取量的倒油问题

    题目描述 [题目描述]  有三个容器,容量分别为 a,b,c(a> b > c ),一开始a装满油,现在问是否只靠abc三个容器量出k升油.如果能就输出"yes",并且 ...

  8. [kuangbin]专题三 Dancing Links Squiggly Sudoku HDU - 4069【DFS】【精确覆盖】

    [题目描述] Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each ...

  9. POJ - 3179 Corral the Cows【离散化】【前缀和】

    [题目描述] Farmer John wishes to build a corral for his cows. Being finicky beasts, they demand that the ...

最新文章

  1. 公务员_只愿与一人十指紧扣_新浪博客
  2. 小程序之 转发/分享
  3. python工程计算软件库_python中常用的科学计算工具包
  4. dart系列之:dart语言中的函数
  5. leetcode 高薪_LeetCode 第 125 号问题:验证回文串
  6. python之做一个简易的翻译器(一)
  7. vue-class-component 以class的模式写vue组件
  8. centos7 卸载 jdk
  9. Java 基础(十九)代理
  10. PaddleNLP Taskflow
  11. 计算机桌面如何分区,讲解电脑如何分区
  12. HDU 5294 - Tricks Device(最短路+最小割)
  13. 完全java实现一款开源的报表工具简表(JOR)
  14. AES与RSA混合加密完整实例
  15. 2023年全国最新工会考试精选真题及答案33
  16. Java获得随机数的几种方法
  17. 超好用的windows远程桌面管理工具Remote Desktop Connection Manager
  18. yum命令的基本用法
  19. 【C++】【读个小故事就弄懂】为什么基类指针可以指向派生类对象,而派生类指针却不可以指向基类对象?(最易懂最有趣最生动的举例)
  20. matlab代码规范(自用)

热门文章

  1. TensorFlowOnSpark 接口函数用法
  2. Kubernetes集群搭建之Etcd集群配置篇
  3. 【转载】RPG模式研究——即时制与回合制战斗对比
  4. 《高级着色语言HLSL入门》系列文章
  5. 很幽默的讲解六种Socket I/O模型C++程序设计
  6. 认识JWT(JSON WEB TOKEN)
  7. java 调用native api_Windows和Native API中的系统调用?
  8. 数据库系统原理笔记:关系数据库设计
  9. %3c %3e 转换html,防止基本的XSS攻击 滤掉HTML标签
  10. php 日志增强,php 日志扩展