DP——划分子集和问题

<span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">对于由从1到N (1 <= N <= 39)这N个连续的整数组成的集合来说,我们有时可以将集合分成两个部分和相同的子集合。</span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">例如,N=3时,可以将集合{1, 2, 3} 分为{1,2}和{3}。此时称有一种方式(即与顺序无关)。</span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">N=7时,共有四种方式可以将集合{1, 2, 3, ..., 7} 分为两个部分和相同的子集合:</span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">{1,6,7} 和 {2,3,4,5} </span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">{2,5,7} 和 {1,3,4,6} </span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">{3,4,7} 和 {1,2,5,6} </span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">{1,2,4,7} 和 {3,5,6} </span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">输入:程序从标准输入读入数据,只有一组测试用例。如上所述的N。</span><br style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);" /><span style="color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 20px; background-color: rgb(228, 230, 233);">输出:方式数。若不存在这样的拆分,则输出0。</span>
</pre><pre name="code" class="cpp">dp[7]=1+dp[6]+2+dp[5]+3+dp[4]+4+dp[3]+5+dp[2]+6+dp[1];
dp[6]=<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">1+dp[5]+2+dp[4]+3+dp[3]+4+dp[2]+5+dp[1];</span>
······
dp[0]=1;只有一种方式——————————个人觉得这种描述方式最简单,刚看见一片繁琐的解释如下:
http://www.cnblogs.com/kangjianwei101/p/5332451.html
</pre><pre name="code" class="cpp">
#include"stdio.h"
#include"string.h"
int main()
{
	int n,i,j,sum;
	long dp[1000];
	memset(dp,0,sizeof(dp));//将数组初始化 
	dp[0]=1;
	scanf("%d",&n);
	sum=n*(n+1);
	if (sum%4) {printf("0\n");return 0;}
	else 
	{
		sum=sum/4;
		for(i=1;i<=n;i++)
			for(j=sum;j>=i;j--)
			dp[j]+=dp[j-i]; 
	}
	printf("%d\n",dp[sum]/2);
}
</pre><pre name="code" class="cpp">
其实还有一个问题,为什么定义为dp[100]的时候就无效内存引用,求大神解释

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值