UVa 1099-point chocolate ACM Problem Solving report (pressure DP)

Source: Internet
Author: User

This cut chocolate, first see this problem data is very small, n is equal to 15, can directly compress the state, the complete is (1<<n)-1, and then take his subset analysis

First of all, the area of the chocolate in each set and find out, Sum[s]=sigma (A[i]),s& (1<<i) ==1;

The state transition equation is DP (s,min (R,SUM[S]/R)) divided into two sub-states DP (S0,min (R,SUM[S0]/R)) and DP (S1,min (R,SUM[S1]/R)), S0+s1=s;sum[s0]+sum[s1]=sum [S]

If two sub-states are true, then the total state is true.

DP Boundary when Bitcount (s) ==1,return 1; If none of the splits are true, then return 0

#include <iostream> #include <cstdio> #include <cctype> #include <cstdlib> #include <cmath > #include <algorithm> #include <cstring> #include <string> #include <vector> #include <
queue> #include <map> #include <set> #include <sstream> #include <stack> using namespace std;
#define MAX 50005 typedef long Long LL;
const double pi=3.141592653589793;
const int INF=1E9;
const double INF=1E20;
const double eps=1e-6;
int a[16],sum[1<<16],vis[1<<16][105],d[1<<16][105];
	int bitcount (int x) {return (x==0) X:bitcount (X/2) + (x&1);} int dp (int s,int x) {if (vis[s][x]) return d[s][x];
	Vis[s][x]=1;
	if (Bitcount (s) ==1) return d[s][x]=1;
	int y=sum[s]/x;
		for (int s0= (s-1) &s;s0;s0= (s0-1) &s) {int s1=s-s0;
		if (SUM[S0]%X==0&AMP;&AMP;DP (S0,min (x,sum[s0]/x)) &AMP;&AMP;DP (S1,min (x,sum[s1]/x))) return d[s][x]=1; if (SUM[S0]%Y==0&AMP;&AMP;DP (S0,min (y,sum[s0]/y)) &AMP;&AMP;DP (S1,min (y,sum[s1]/y))) return d[s][x]= 1;
} return d[s][x]=0;
	} int main () {int n,kase=0;
		while (cin>>n&&n) {kase++;
		int x, y;
		scanf ("%d%d", &x,&y);
		memset (sum,0,sizeof (sum));
		memset (vis,0,sizeof (VIS));
		for (int i=0;i<n;i++) scanf ("%d", &a[i]);
			for (int s=1;s< (1<<n), s++) {for (int i=0;i<n;i++) {if (s& (1<<i)) sum[s]+=a[i];
		}} int all= (1<<n)-1;
		int ans;
		if (sum[all]!=x*y) ans=0;
		Else ANS=DP (all,min (x, y));
		if (ans) printf ("Case%d:yes\n", Kase);
	else printf ("Case%d:no\n", Kase);
} return 0;

 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.