【动态规划】[UVa 1099]Sharing Chocolate
实际上就是另
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 15;
const int MAXD = 100;
int S[MAXN+10], Sum[(1<<MAXN)+2];
int f[(1<<MAXN)+2][MAXD+3];
inline int bitcount(int r){
int ret = 0;
while(r){
if(r & 1) ret++;
r >>= 1;
}
return ret;
}
int dp(int s, int r){
int &ans = f[s][r];
if(ans != -1) return ans;
if(bitcount(s) == 1) return ans=1;
int c = Sum[s] / r;
for(int s0=(s-1)&s; s0; s0=(s0-1)&s){
int _s = s-s0;
if(Sum[s0]%r == 0 && dp(s0, min(r, Sum[s0]/r)) && dp(_s, Sum[_s]/r)) return ans=1;
if(Sum[s0]%c == 0 && dp(s0, min(r, Sum[s0]/c)) && dp(_s, Sum[_s]/c)) return ans=1;
}
return ans=0;
}
int main(){
int n, a, b, ccnt=0;
while(scanf("%d", &n) != EOF && n){
memset(f, -1, sizeof f);
memset(Sum, 0, sizeof Sum);
scanf("%d%d", &a, &b);
if(a > b) swap(a, b);
for(int i=0;i<n;i++)
scanf("%d", &S[i]);
int Max=1<<n;
for(int S1=1; S1<Max; S1++)
for(int i=0;i<n;i++)
if(S1&(1<<i))
Sum[S1] += S[i];
if(Sum[Max-1]%a == 0 && Sum[Max-1] == a * b) dp(Max-1, a);
printf("Case %d: %s\n", ++ccnt, f[(1<<n)-1][a] > 0 ? "Yes" : "No");
}
return 0;
}