pku_Dividing_1014

/*
  Name: Dividing - PKU 1014
  Author: Mars
  Description: DP
  Result:    2231562 bestzsx 1014 Accepted 520K 421MS C++ 1617B 2007-05-29 21:30:22
*/
 
#include <iostream>
#include <string>

using namespace std;

const int N = 6;

int c[N+1];
bool st[N+1][10000*N];
int maxItem[N+1];

int total;

string solvable = "Can be divided./n";
string unsolvable = "Can't be divided./n";

int _main() {
 int i,j,k,h;
 int flag;
 int cases=0;

 while (true) {
  flag = 0;
  total = 0;
  for (i=1;i<=N;++i) {
   cin>>c[i];
   if (c[i]>0) {
    flag = 1;
    total += i*c[i];
   }
  }
  if (!flag) break;
  ++cases;
  if (cases>1) cout<<"/n";
  cout<<"Collection #"<<cases<<":/n";
  // solve now; flag = 1
  memset(st,0,sizeof(st));
  if (total % 2) {
   flag = 0;
   cout<<unsolvable;
  } else {
   total = total / 2;
   // state 1
   for (i=0;i<=c[1];++i) {
    st[1][i] = true;
   }
   c[0] = 0;
   for (i=1;i<=N;++i) {
    maxItem[i] = i*c[i]+maxItem[i-1];
    if (maxItem[i]>total) maxItem[i] = total;
   }
   //
   flag = 0;
   for (i=2;i<=5;++i) {
    for (k=maxItem[i];k>=0;--k) {
     if (st[i][k]) continue;
     j = k/i;
     if (j>c[i]) j = c[i];
     for (;j>=0;--j) {
      if (st[i-1][k-i*j]) {
       for (h=0;h<=j;++h)
        st[i][k-i*h] = true;
       break;
      }
     }
     if (st[i][total]) {
      flag = 1;
      break;
     }
    }
    if (flag) break;
   }
   //
   if (flag) {
    cout<<solvable;
    continue;
   }
   // state 6
   for (j=0;j<=c[6];++j) {
    if ((total-6*j)<0)
     break;
    else if (st[5][total-6*j]) {
     st[6][total] = true;
     break;
    }
   }
   if (st[6][total]) cout<<solvable;
   else cout<<unsolvable;
  }
 }

 return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值