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$更小,这个也都能理解吧。

这样的话就应该能过了。

然后款搜的时候要注意下,枚举出一个二元组能变换出来的所有可能的二元组,这个具体可以看代码。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<queue>
  5. using namespace std;
  6. const int N = , SIZE = 1e6 + ;
  7. int n;
  8. struct node{
  9. int x, y, g, h;
  10. bool operator < (const node &a)const{
  11. return g + h == a.g + a.h ? h > a.h : g + h > a.g + a.h;
  12. }
  13. };
  14. struct Node{
  15. int to, next, w;
  16. };
  17. struct hash_map{
  18. int head[N], now;
  19. Node a[SIZE];
  20. bool insert(int sta, int w){
  21. int x = sta % N;
  22. for(int i = head[x]; i; i = a[i].next){
  23. if(a[i].to == sta){
  24. if(a[i].w <= w) return ;
  25. a[i].w = w; return ;
  26. }
  27. }
  28. a[++now] = {sta, head[x], w};
  29. head[x] = now;
  30. return ;
  31. }
  32. }dict;
  33. priority_queue<node> heap;
  34. node now;
  35. int gcd(int a, int b){ return b ? gcd(b, a % b) : a;}
  36. void che(int x, int y){
  37. if(x < y) swap(x, y);
  38. if(x > * n) return ;
  39. if(x > n && y == ) return ;
  40. if(x == y) return ;
  41. if(n % gcd(x, y)) return;
  42. if(!dict.insert(x * + y, now.g + )) return;
  43. int h = , tx = x;
  44. while(tx < n) h++, tx <<= ;
  45. heap.push({x, y, now.g + , h});
  46. }
  47. void A_star(){
  48. heap.push({, , , });
  49. while(!heap.empty()){
  50. now = heap.top(); heap.pop();
  51. if(now.x == n || now.y == n){
  52. printf("%d\n", now.g); break;
  53. }
  54. int a[] = {now.x, now.y};
  55. for(int i = ; i < ; i++)
  56. for(int j = i; j < ; j++)
  57. for(int k = ; k < ; k++){
  58. int b[] = {a[], a[]};
  59. b[k] = a[i] + a[j];
  60. che(b[], b[]);
  61. }
  62. che(now.x - now.y, now.y);
  63. che(now.x, now.x - now.y);
  64. }
  65. }
  66. int main(){
  67. scanf("%d", &n);
  68. A_star();
  69. return ;
  70. }

poj 1945 Power Hungry Cows A*的更多相关文章

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

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

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

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

  3. 【BFS】Power Hungry Cows

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

  4. 贪心 POJ 2109 Power of Cryptography

    题目地址:http://poj.org/problem?id=2109 /* 题意:k ^ n = p,求k 1. double + pow:因为double装得下p,k = pow (p, 1 / ...

  5. BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛

    1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 665  Solved: 419 ...

  6. BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )

    裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...

  7. 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 ...

  8. 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 ...

  9. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

随机推荐

  1. P/Invoke Interop Assistant工具C到C#自动转换

    C#互操作的类型基本位于System.Runtime.InteropServices命名空间下,本系列随笔主要记录本人在开发过程中使用的到一些类型函数.技巧及工具 计算类型的大小 int size = ...

  2. JMeter学习工具简单介绍

    JMeter学习工具简单介绍   一.JMeter 介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态 ...

  3. 列出连通集(mooc)

    给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...

  4. 高可用Kubernetes集群-4. kubectl客户端工具

    六.部署kubectl客户端工具 1. 下载 [root@kubenode1 ~]# cd /usr/local/src/ [root@kubenode1 src]# wget https://sto ...

  5. Hyperledger中的共识机制

    Hyperledger Consensus 共识过程 Hyperlydger中建立共识的过程由以下两个独立的过程构成: Ordering of transactions (交易排序) Validati ...

  6. spring-boot Jpa配置

    spring.jpa.hibernate.ddl-auto ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空 ddl-auto:create-drop---- ...

  7. node项目设置环境变量

    在UNIX系统中: $ NODE_ENV=production node app 在Windows中: $ set NODE_ENV=production $ node app 这些环境变量会出现在程 ...

  8. ECharts之force力导向布局图——数据源说明及后端API约定

    Echarts ? 关于 Echarts 请移步这里 force 力导向图 实现方式,如: function require_EC () { require( [ 'echarts', //载入for ...

  9. Planning The Expedition(暴力枚举+map迭代器)

    Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...

  10. 第六次作业psp

    psp 进度条 代码累积折线图 博文累积折线图 psp饼状图