hdu 1208 Ignatius and the Princess III 划分数,dp
题意:给你一个数字n,求将其划分成若干个数字相加总共有多少种划分数;
<span style="font-size:24px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
int dp[125][125];
//dp[i][j]=dp[i][j-1]+dp[i-j][j]
int main()
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=120;i++)
for(int j=1;j<=120;j++)
if(j<i)
dp[i][j]=dp[i][j-1]+dp[i-j][j];
else if(j>i)
dp[i][j]=dp[i][i];
else if(i==j)
dp[i][j]=dp[i][j-1]+1;
int n;
while(cin>>n)
cout<<dp[n][n]<<endl;
return 0;
}
</span>分析:dp;
核心代码:dp[i][j]=dp[i][j-1]+dp[i-j][j]
dp[i][j]代表第i个数划分数中最大不超过j的划分种类数
if(j>i)dp[i][j]=dp[i][i];
如果j==i;dp[i][j]=dp[i][j-1]+1;
if(j<i) dp[i][j]=dp[i][j-1](没有j)+dp[i-j][j](有j,那么取出j,则剩余的划分数中最大也不
能超过j,所以是dp[i-j][j])
你说,我们都会幸福的,对吧?

浙公网安备 33010602011771号