【BFS】Power Hungry Cows
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5522 | Accepted: 1384 |
Description
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
Output
Sample Input
31
Sample Output
6
Source
#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的更多相关文章
- [USACO2002][poj1945]Power Hungry Cows(启发式搜索)
Power Hungry CowsTime Limit: 1000MS Memory Limit: 30000K Total Submissions: 4570 Accepted: 1120 Desc ...
- 『Power Hungry Cows A*启发式搜索』
Power Hungry Cows(POJ 1945) Description FJ的奶牛想要快速计算整数P的幂 (1 <= P <=20,000),它们需要你的帮助.因为计算极大数的幂, ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- 【CF913G】Power Substring 数论+原根
[CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
随机推荐
- 发福利喽稀疏FFT
附介绍: 四位来自麻省理工学院的研究人员蒂娜·卡塔比(Dina Katabi).海塞姆·哈桑(Haitham Hassanieh).比欧特·因迪克(Piotr Indyk)和埃里克·普里斯(Eric ...
- SQL语句中的单引号处理以及模糊查询
为了防止程序SQL语句错误以及SQL注入,单引号必须经过处理.有2种办法: 1.使用参数,比如SELECT * FROM yourTable WHERE name = @name; 在C#中使用Sql ...
- import学习
一.import as import socket, os, regex模块导入时可以使用 as 关键字来改变模块的引用对象名字: import os as system //当多个引入时 ...
- 通过实例来学习XML DTD
使用DTD的原因: 注意:由于它自身的一些缺点,DTD终将被淘汰,但是它还是要学习的.学习完DTD后,后面继续学习XML Schema. 1,通过 DTD,您的每一个 XML 文件均可携带一个有关其自 ...
- 【目录】Python模块学习系列
目录:Python模块学习笔记 1.Python模块学习 - Paramiko - 主机管理 2.Python模块学习 - Fileinput - 读取文件 3.Python模块学习 - Confi ...
- Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)【转】
转自:http://www.linuxidc.com/Linux/2013-06/85221p3.htm 阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段汇编代码的需求,这种嵌入 ...
- 81.Search in Rotated Sorted Array II---二分变形
题目链接 题目大意:与33题类似,只是这里数组中有重复数值. 法一:解法与33题类似,只是这里要处理1,3,1,1,1这种情况,即有重复值时,mid与left和right都相等时,可以采用right- ...
- JSP(3) - 9个JSP内置对象 - 小易Java笔记
1.9个JSP内置对象 内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse config Servle ...
- 使用Webpack搭建Vue项目
前提: 1. 借助Node.js环境里的npm来安装, 2. 设置好npm镜像, (比如淘宝的npm镜像:输入 引用 npm install -g cnpm –registry=https://r ...
- Android Studio代码字体模糊解决方法
问题描述: 我的电脑分辨率是(1920*1080),然而安装了Android Studio后代码的一些部分区域出现模糊的现象,应该是软件默认设置与高分屏有冲突. 如下图所示. 解决方法: 打开设置对话 ...