51NOD 1201 整数划分

xiaoxiao2021-02-28  66

题目链接:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201

题解:

一道很好的dp题目,首先我们先要明确dp的含义。这里开的是二维dp,表示i这个数划分为j个数的方案数。(这里j表示的不是max值) 当我们对其进行划分的时候,可以这么去想,我们要把一个数划分为4的时候,那么就相当于在(N-4)这个的基础上这些数进行加1的操作。但是坑点就是遇到0的时候,需要去掉它。 因此,我们可以推断出这个的转移方程为:dp[i][j]=dp[i-j][j]+d[i-j][j-1];后者是指遇到0的情况的时候,去掉0。

代码:

#include <cmath> #include <cstdio> #include <map> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define met(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f const int maxn =5e4+10; const int mod = 1e9+7; int dp[maxn][320]; int main() { int n; scanf("%d",&n); met(dp,0); dp[0][0]=1; for(int i=1;i<=n;i++) { for(int j=1;j*j<2*i;j++) { dp[i][j]=(dp[i-j][j-1]+dp[i-j][j])%mod; } } int ans=0; for(int i=1;i<=n;i++) { ans=(ans+dp[n][i])%mod; } printf("%d\n",ans); }
转载请注明原文地址: https://www.6miu.com/read-53190.html

最新回复(0)