51nod 1597 有限背包计数问题 DP 根号分治

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_36797646/article/details/83757656

题解:

考虑根号分治。
对于体积 n \le\sqrt n 的东西,发现背包可以用一个前缀和优化。
对于体积 > n >\sqrt n 的东西,实际上每个物品都可以看做有无限个 ,就是求把某个数分成若干份,每份都至少为 n + 1 \sqrt n+1 的方案数,由于分成的份数不会超过 n \sqrt n ,所以用一个简单的DP就行了。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxn=100010;
const int inf=2147483647;
const int mod=23333333;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
int n,f[Maxn],g[2][Maxn],s[Maxn],ff[Maxn];
void upd(int &x,int y){x+=y;if(x>=mod)x-=mod;}
int main()
{
	memset(ff,0,sizeof(ff));
	memset(f,0,sizeof(f));
	n=read();int m=(int)sqrt(n);
	f[0]=1;
	for(int i=0;i<=n;i++)s[i]=1;
	for(int i=1;i<=m;i++)
	{
		for(int j=n;j>=i;j--)
		{
			int k=min(i,j/i);
			upd(f[j],(s[j-i]-((j-(k+1)*i>=0)?s[j-(k+1)*i]:0)+mod)%mod);
		}
		s[0]=1;
		for(int j=1;j<=n;j++)s[j]=(f[j]+((j>=i+1)?s[j-(i+1)]:0))%mod;
	}
	int now=0,ans=0;
	memset(g[now],0,sizeof(now));
	g[0][0]=1;
	for(int i=1;i<=m;i++)
	{
		now^=1;
		memset(g[now],0,sizeof(g[now]));
		for(int j=i;j<=n-m*i;j++)
		{
			upd(g[now][j],g[now^1][j-1]),upd(g[now][j],g[now][j-i]);
			upd(ans,(LL)g[now][j]*f[n-i*m-j]%mod);
		}
	}
	upd(ans,f[n]);
	printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/baidu_36797646/article/details/83757656