poj 1945 Power Hungry Cows A*
Description:
就是给你一个数,你可以把它自乘,也可以把他乘或除以任意一个造出过的数,问你最多经过多少次操作能变换成目标数
思路:这题真的不怎么会啊。n = 20000,每一层都有很多个扩展状态,裸宽搜会被T,启发式函数又设计不出来……
看了一个Vjudge上的代码才知道这题怎么写。
就是每一个状态是由最多两个数转化而来的,所以可以把两个数看做一个状态。
用一个多元组$node(x,y,g,h)$表示状态,$x, y$分别表示两个数中的较大数和较小数,然后$g$表示转换成当前的状态需要多少步,$h$表示大数$x$转换到大于等于目标状态至少还要多少步。
启发式函数就是当前步数+预期至少需要的步数,即$g+h$
再用一个哈希表把二元组$(x,y)$与转换到这个状态需要几步对应起来,这样可以完成去重。当然也可以用$map$实现,但按照poj的尿性,很可能TLE。。
然后加几个剪枝,排除以下多余状态:
1.如果$x > 2*n$,这个都能理解吧。
2.如果$x=y$,因为该状态和一个$x$的状态对未来的贡献是等价的,反正自乘自除也能达到一样的效果,不管$y$取什么数,都比$x$与$y$相等时更优。
3.如果$x > n$ 并且 $y = 0$,因为这样的话该状态永远达不到$x=n$。
4.如果$n $ $mod$ $gcd(x,y) != 0$,因为这样的状态不管怎么乘怎么除,也永远达不到$x=n$。
5.如果$(x,y)$已经在哈希表里了且对应的$g$更小,这个也都能理解吧。
这样的话就应该能过了。
然后款搜的时候要注意下,枚举出一个二元组能变换出来的所有可能的二元组,这个具体可以看代码。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N = , SIZE = 1e6 + ;
int n;
struct node{
int x, y, g, h;
bool operator < (const node &a)const{
return g + h == a.g + a.h ? h > a.h : g + h > a.g + a.h;
}
};
struct Node{
int to, next, w;
};
struct hash_map{
int head[N], now;
Node a[SIZE];
bool insert(int sta, int w){
int x = sta % N;
for(int i = head[x]; i; i = a[i].next){
if(a[i].to == sta){
if(a[i].w <= w) return ;
a[i].w = w; return ;
}
}
a[++now] = {sta, head[x], w};
head[x] = now;
return ;
}
}dict;
priority_queue<node> heap;
node now;
int gcd(int a, int b){ return b ? gcd(b, a % b) : a;}
void che(int x, int y){
if(x < y) swap(x, y);
if(x > * n) return ;
if(x > n && y == ) return ;
if(x == y) return ;
if(n % gcd(x, y)) return;
if(!dict.insert(x * + y, now.g + )) return;
int h = , tx = x;
while(tx < n) h++, tx <<= ;
heap.push({x, y, now.g + , h});
}
void A_star(){
heap.push({, , , });
while(!heap.empty()){
now = heap.top(); heap.pop();
if(now.x == n || now.y == n){
printf("%d\n", now.g); break;
}
int a[] = {now.x, now.y};
for(int i = ; i < ; i++)
for(int j = i; j < ; j++)
for(int k = ; k < ; k++){
int b[] = {a[], a[]};
b[k] = a[i] + a[j];
che(b[], b[]);
}
che(now.x - now.y, now.y);
che(now.x, now.x - now.y);
}
}
int main(){
scanf("%d", &n);
A_star();
return ;
}
poj 1945 Power Hungry Cows A*的更多相关文章
- 『Power Hungry Cows A*启发式搜索』
Power Hungry Cows(POJ 1945) Description FJ的奶牛想要快速计算整数P的幂 (1 <= P <=20,000),它们需要你的帮助.因为计算极大数的幂, ...
- [USACO2002][poj1945]Power Hungry Cows(启发式搜索)
Power Hungry CowsTime Limit: 1000MS Memory Limit: 30000K Total Submissions: 4570 Accepted: 1120 Desc ...
- 【BFS】Power Hungry Cows
Power Hungry Cows Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5522 Accepted: 1384 ...
- 贪心 POJ 2109 Power of Cryptography
题目地址:http://poj.org/problem?id=2109 /* 题意:k ^ n = p,求k 1. double + pow:因为double装得下p,k = pow (p, 1 / ...
- BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛
1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 665 Solved: 419 ...
- BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )
裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
随机推荐
- HTTP结构讲解——《HTTP权威指南》系列
HTTP结构 第二部分的5章主要介绍了HTTP服务器,代理,缓存,网关和机器人应用程序,这些都是Web系统架构的构造模块. Web服务器 第五章 Web服务器会对HTTP请求进行处理并提供响应.术语& ...
- MongoDB 极简实践入门
原作者StevenSLXie; 原链接(https://github.com/StevenSLXie/Tutorials-for-Web-Developers/blob/master/MongoDB% ...
- 【TCP_协议_socket接口】-jmeter
1.ip 2.端口号 3.传入参数 4.告诉软件返回 最后以为是什么,不然就会报错 或者无限制的等待 查ascll 码表 启动接口的方法
- 禁用 Python GC,Instagram 性能提升10%
通过关闭 Python 垃圾收集(GC)机制,该机制通过收集和释放未使用的数据来回收内存,Instagram 的运行效率提高了 10 %.是的,你没听错!通过禁用 GC,我们可以减少内存占用并提高 C ...
- PHP开发中常见的漏洞及防范
PHP开发中常见的漏洞及防范 对于PHP的漏洞,目前常见的漏洞有五种.分别是Session文件漏洞.SQL注入漏洞.脚本命令执行漏洞.全局变量漏洞和文件漏洞.这里分别对这些漏洞进行简要的介绍和防范. ...
- 作业 20181023-11 Alpha发布
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2283 队名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙赛佳 ...
- Scrum立会报告+燃尽图(Beta阶段第七次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2388 项目地址:https://coding.net/u/wuyy694 ...
- 【探路者】团队中的每一次感动——Alpha版
我是[探路者]团队的leader翟宇豪.在软件工程课程开始时,当听说有团队作业这个任务时,我个人还是对leader这个角色很期待的.我很希望通过自己的努力,让我所在的团队变得更好,让组里的每一个成员在 ...
- 20172330 2017-2018-1 《Java程序设计》第十周学习总结
20172330 2017-2018-1 <程序设计与数据结构>第十周学习总结 教材学习内容总结 本周的学习内容为集合 集合 对象具有定义良好的接口,从而成为一种实现集合的完善体制. 动态 ...
- CodeForces 479C Exams 贪心
题目: C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input o ...