首页 > 其他分享 >Poj 3134 Power Calculus(IDA*)

Poj 3134 Power Calculus(IDA*)

时间:2024-02-17 19:56:44浏览次数:35  
标签:Calculus val int pos 3134 depth Poj

3134 -- Power Calculus (poj.org)

相当于是问1经过多少次能变成n,val[pos]<<(depth-now)为估计函数,如果最快都不能到n,就return false

#include<iostream>
using namespace std;
const int N=1010;
int n,pos,val[N];
bool IDAstar(int now,int depth){
    if(now>depth) return false;
    if(val[pos]<<(depth-now)<n) return false;
    if(val[pos]==n) return true;
    pos++;
    for(int i=0;i<pos;i++){
        val[pos]=val[pos-1]+val[i];
        if(IDAstar(now+1,depth)) return true;
        val[pos]=abs(val[pos-1]-val[i]);
        if(IDAstar(now+1,depth)) return true;
    }
    pos--;
    return false;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    while(cin>>n && n){
        int depth;
        for(depth=0;;depth++){
            pos=0;
            val[pos]=1;
            if(IDAstar(0,depth)) break;
        }    
        cout<<depth<<endl;
    }
    return 0;
}

标签:Calculus,val,int,pos,3134,depth,Poj
From: https://www.cnblogs.com/accbulb/p/18018270

相关文章

  • POJ--3614 Sunscreen(贪心)
    记录18:262024-2-15http://poj.org/problem?id=3614贪心法,将minspf从大到小排列,然后选取最大的spf点击查看代码#include<iostream>#include<vector>#include<algorithm>#include<utility>#include<stdio.h>#include<string.h>usingnamespacestd;......
  • poj 2676 Sudoku(DFS+回溯+剪枝)
    2676--Sudoku(poj.org)#include<iostream>#include<cstring>usingnamespacestd;intt,row[10][10],col[10][10],grid[10][10],map[10][10];boolDFS(intr,intc){if(r==10)returntrue;boolflag=false;if(map[r][c]){if(c=......
  • poj 1416 Shredding Company
    1416--ShreddingCompany(poj.org)#include<iostream>#include<cstring>usingnamespacestd;charnum[10];intt,max_cnt,max_sum,shred_cnt,ans[10],tmp_ans[10];//目标,最大值个数,最大值,切割次数,最后答案,临时答案voidDFS(intbegin,intnow_sum,intcnt){//切的位......
  • Poj 2531 Network Saboteur(DFS+剪枝)
    2531--NetworkSaboteur(poj.org)#include<iostream>#include<cstring>usingnamespacestd;constintN=30;intC[N][N],n,ans;boolgroup[N];voidDFS(intnum,intsum){group[num]=true;inttmp=sum;for(inti=1;i<=n;i++){......
  • POJ--1179 Polygon(区间DP)
    记录22:012024-2-10http://poj.org/problem?id=1179区间DP问题。区间DP问题可能需要注意的点就是是根据区间长度来计算的,随着迭代区间长度不断增加,结果也就计算出来了这种“任意选择一个位置断开,复制形成2倍长度的链”的方法,是解决DP中环形结构的常用手段之一因此读入数......
  • POJ--3764 The xor-longest Path(Trie)
    记录13:562024-2-10找到俩个点,获得最大的边权异或值。利用异或的性质,一个值被异或俩次相当于没有异或即axorbxorb=a所以先从顶点出发,获得每个点路径上的异或值,然后对这俩个值进行异或就获得了他们之间路径的异或值。获取从顶点到每个点路径上的异或值后,可以利用trie来......
  • Poj 1077 Eight!(BFS模板解法)
    吃完晚饭,啃着tomato来poj上提交,结果不支持unordered_map,吐血啦,看来还是要用BFS+康托展开,还想再写一篇双向BFS的,对这道题算是圆满了*_*,但是要用G++提交,C++会报错我也不知道为嘛#include<iostream>#include<cstring>#include<queue>#include<unordered_map>usingnamespaces......
  • Poj 1077 Eight!(BFS-A*)
    1077--Eight(poj.org)由结论可以知道逆序对为奇数的时候无解,f为估值函数,计算曼哈顿距离。想用康托展开写,但多状态数码问题用数组存储状态不够,有TLE的风险,还是A*吧!吃一个tomato宣告今日...不知道结不结束#include<iostream>#include<vector>#include<queue>#include......
  • Poj 3414 Pots (BFS+回溯+队列)
    这道题需要输出最后结果的执行过程,可以通过结构体,在结构体中定义一个数组s,s中存储了每一步的执行过程,实现了回溯。并且在运行中可以适当剪枝,减少枚举次数。 #include<iostream>#include<queue>#include<cstring>usingnamespacestd;constintN=110;intaa,bb,cc,vis[N......
  • 洛谷P5907 数列求和加强版 / SPOJ MOON4
    题面描述求\[\sum_{i=1}^ni^ka^i\]对\(998244353\)取模的结果。\(O(k)\)做法为了推导方便,下令\(p=a^{-1}\)。即求\[\sum_{i=1}^n\frac{i^k}{p^i}\]我们裂项,即尝试寻找多项式\(f(x)\),使得\[\frac{x^k}{p^x}=\frac{f(x)}{p^{x-1}}-\frac{f(x+1)}{p^x}\]即\[x^k......