问一个问题
查看原帖
问一个问题
384064
kevin985楼主2021/7/8 18:34

加上一维数组优化后为什么要把结果除以2才能通过

#include <bits/stdc++.h>
#include <cstring>
#define INF 0x7f7f7f7f
#define eps 1e-6
#define ll long long
#define ull unsigned long long
#define N 40
using namespace std;
ll n,tot;
ll dp[(N+1)*N/2+10];
inline ll read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
int main()
{
//	std::ios::sync_with_stdio(false);
	n = read();
	tot = (1 + n) *n / 2;
	if(tot % 2 == 1)
	{
		printf("0");
		return 0;
	}
	dp[0] = 1;
	for(register ll i=1;i<=n;++i)
	{
		for(register ll j=tot;j>=1;--j)
		{
			if(j >= i) dp[j] = dp[j] + dp[j-i];
		}
	}
	cout<<dp[tot/2]/2;
	return 0;
}
2021/7/8 18:34
加载中...