题目大意:求N的满足下列条件的划分的个数。划分中各个数互不相等。划分中至少有两个元素。
Time Limit:1000MS Memory Limit:16384KB 64bit IO Format:%I64d & %I64u
数据规模:5<=N<=500。
理论基础:正整数的划分:如果n=n1+n2+n3+...+nk(1<=n1<=n2<=...<=nk,k>=1)则称(n1,n2,...,nk)是正整数n的一个划分。
题目分析:典型的dp规划题目。用dp[i][j]表示正整数i的划分中含有j个不相等元素的个数。
首先:初始化,虽然题目要求至少两个元素,但是现在我们忽略它,那么最后的答案就是sum(dp[n][j](2<=j&&j*(j+1)/2<=i)),所以初始化时dp[i][1]全部初始化为1。
其次;我们探寻状态转移方程。对于dp[i][j],我们思考它只有两种划分的可能,一种是划分中每个数都大于1,这种可能的数目为dp[i-j][j](对每个数都减去1)。第二种情况,划分中有一个数为1,那么我们把每个数减1就有dp[i-j][j-1]。除此之外,再无重复,而且没有遗漏的情况。所以,dp[i][j]=dp[i-j][j]+dp[i-j][j-1]。
最后,再考虑一些细节的东西就ok了。
代码如下:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 1
#define maa (1<<31)
#define mii ((1<<31)-1)
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
}
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
typedef long long LL;
typedef long unsigned int LU;
typedef long long unsigned int LLU;
#define N 500
#define M 32
LL dp[N+1][M+1],ans;
int n;
int main()
{
while(~scanf("%d",&n))
{
memset(dp,0,sizeof dp);
ans=0;
int k=(int)ceil((sqrt((double)(1+8*n))-1)/2);
for(int i=1;i<=n;i++)dp[i][1]=1;
for(int i=1;i<=k;i++)
{
for(int j=i+1;j<=n;j++)
{
dp[j][i]=dp[j-i][i-1]+dp[j-i][i];
}
}
for(int j=2;j<=k;j++)ans+=dp[n][j];
printf("%I64d\n",ans);
}
return 0;
}
其中,32也是预先计算出来的,32*33/2>500。
by:Jsun_moon http://blog.csdn.net/Jsun_moon