• UVA529 Addition Chains


    嘟嘟嘟


    还是(IDA*)
    这道题是(ZOJ)的加强版,(n)(100)扩大到了(10000),所以必须有非常给力的剪枝才能过。
    除了迭代加深,还要加上估价函数:对于当前数(x)(h(x))应该是(O(log_{2}{x})),即每一次否给(x)(2)
    然后如果这么手动取乘(2)的话,注意得开(long long),要不然会出现负数而(T)飞的……

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e4 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, dep = 0, a[100];
    bool dfs(int step)
    {
      if(step == dep) return a[step] == n ? 1 : 0;
      for(int i = 0; i <= step; ++i)
        for(int j = i; j <= step; ++j)
          {
    	if(a[i] + a[j] > n || a[i] + a[j] <= a[step]) continue;
    	ll sum = a[i] + a[j];
    	for(int k = step + 2; k <= dep; ++k) sum <<= 1;
    	if(sum < n) continue;
    	a[step + 1] = a[i] + a[j];
    	if(dfs(step + 1)) return 1;
          }
      return 0;
    }
    
    int main()
    {
      while(scanf("%d", &n) && n)
        {
          Mem(a, 0); a[0] = 1;
          int tp = n; dep = 0;
          while(tp >>= 1) dep++;
          for(; !dfs(0); dep++);
          write(a[0]);
          for(int i = 1; i <= dep; ++i) space, write(a[i]); enter;
        }
      return 0;
    }
    
  • 相关阅读:
    使用Objectivec Block
    NSBundle的使用,注意mainBundle和Custom Bundle的区别
    根据名称加载界面
    java获取运行的jar(class)文件的路径
    xamppcontrol中启动Apache时80断口被占用的错误
    [转]利用location的hash值来解决Ajax两大难题
    JAVA 调用Web Service的方法
    asp.net mail
    很是无奈
    憨山大师的醒世咏
  • 原文地址:https://www.cnblogs.com/mrclr/p/10021679.html
Copyright © 2020-2023  润新知