码迷,mamicode.com
首页 > 其他好文 > 详细

UVA529 Addition Chains

时间:2018-11-26 20:13:05      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:while   eof   必须   mat   sdi   void   scanf   scan   get   

嘟嘟嘟


还是\(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;
}

UVA529 Addition Chains

标签:while   eof   必须   mat   sdi   void   scanf   scan   get   

原文地址:https://www.cnblogs.com/mrclr/p/10021679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!