关于记搜
查看原帖
关于记搜
781046
tai_chi楼主2022/8/28 15:46

发现把记忆化放在不同的地方会导致不同的结果,不明白求解释qwq

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=105,mod=1e6+7;
int n,m,a[maxn],dp[maxn][maxn];
int dfs(int depth,int num)
{
	
	if(num==m)
		return 1;
	if(num>m||depth==n+1)
		return 0;
	if(dp[depth][num])//记忆化放这里能ac
		return dp[depth][num];
	int ret=0;
	for(int i=0;i<=a[depth];i++)
	{
		ret=ret+dfs(depth+1,num+i);
		ret%=mod;
	}
	dp[depth][num]=ret;
	return ret;
}
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	cout<<dfs(1,0);
	return 0;
}

记忆化换个位置就wa

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=105,mod=1e6+7;
int n,m,a[maxn],dp[maxn][maxn];
int dfs(int depth,int num)
{
	if(dp[depth][num])//记忆化放这里wa=60
		return dp[depth][num];
	if(num==m)
		return 1;
	if(num>m||depth==n+1)
		return 0;
	
	int ret=0;
	for(int i=0;i<=a[depth];i++)
	{
		ret=ret+dfs(depth+1,num+i);
		ret%=mod;
	}
	dp[depth][num]=ret;
	return ret;
}
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	cout<<dfs(1,0);
	return 0;
}
2022/8/28 15:46
加载中...