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

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

 
 
题目大意:两个容器,每次可以把两个相乘/相除放到某一个容器中,问最少几次能得到x^N? (poj1945)
 
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M,K;
bool vis[201][200001];
struct data{
int x,y,st;
}Que[20000001];
bool flag=false;
int l=1,r=1;
int ans;
void dfs(int a,int b,int c){
if(a>b) swap(a,b);
if(flag||a>200||b>20000) return ;//这里要判一下最优
if(!vis[a][b]){
vis[a][b]=true;
Que[++r].x=a,Que[r].y=b;
Que[r].st=c;
if(!flag&&(a==K||b==K)){
flag=true;
printf("%d\n",c);
}
}
}
void bfs(){
Que[1].x=0,Que[1].y=1,Que[1].st=0;
int step;
while(l<=r){
step=Que[l].st,N=Que[l].x,M=Que[l].y;
dfs(N,N+N,step+1);
dfs(N,M+M,step+1);
dfs(M+M,M,step+1);
dfs(N+N,M,step+1);
dfs(N,M+N,step+1);
dfs(N+M,M,step+1);
dfs(M-N,M,step+1);
dfs(N,M-N,step+1);
l++;
if(flag) return ;
}
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
K=read();
bfs();
return 0;
}

  

【BFS】Power Hungry Cows的更多相关文章

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

    Power Hungry CowsTime Limit: 1000MS Memory Limit: 30000K Total Submissions: 4570 Accepted: 1120 Desc ...

  2. 『Power Hungry Cows A*启发式搜索』

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

  3. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  4. 【CF913G】Power Substring 数论+原根

    [CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...

  5. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  6. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  7. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  8. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  9. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

随机推荐

  1. setsockopt 详解

    1. closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket: BOOL bReuseaddr=TRUE; setsockopt(s,SOL_SOCKET  ...

  2. [bzoj1070] 修车

    这周学习了费用流,就写了几题.其中有一题就是bzoj上的修车,看起来很丧,交了6次都是除了样例全wa(事实证明样例说明不了什么,还会误导你……). 题目大意:有m个技术人员n辆车,一个技术人员只能同时 ...

  3. 20179205《Linux内核原理与分析》第一周作业

    输出 shiyanlou 图形字符的命令banner: 新建用户wangyazhe,输入密码不会显示出来: 利用sudo adduser添加一个用户 loutest,mkdir创建一个新的文件夹opt ...

  4. Perl6 Bailador框架(1):开始

    use v6; use Bailador; get '/' => sub { '<h1><center>Hello, World</center></h ...

  5. Win10打开照片提示“无效的注册表值”解决方法

    1.点开开始菜单,右键单击,选择“以管理员运行”[键盘win键+R]输入PowerShell. 2.输入Get-AppxPackage *photo* | Remove-AppxPackage后回车. ...

  6. 自定义shell开头PS1

    vim /etc/profile export PS1="flag:\W \u\$" \h是主机名,并不全,域 \W是当前所在目录名 \u 是当前shell用户名

  7. HDU 6109 数据分割 并查集,SET

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6109 题意:中文题面 解法:每次都贪心地尝试将尽量多的条件放进当前这组,遇到第一个与已有条件冲突时,就 ...

  8. Centos7 IP地址配置方法

    1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下内容 BOOTPROTO=”static” #dhcp ...

  9. jquery - 实例1

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="text2.aspx.cs& ...

  10. NOIP 2010

    tags: NOIP 并查集 动态规划 搜索 categories: 信息学竞赛 总结 机器翻译 乌龟棋 关押罪犯 引水入城 机器翻译 Solution 维护一个队列, 每次从词典中查词时将单词加入队 ...