标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17917 Accepted Submission(s): 12558
import java.util.*; import java.io.*; public class Main { public static int cal(int n) { int c1[]=new int [n+1]; int c2[]=new int [n+1]; for(int i=0;i<=n;i++) { c1[i]=1; c2[i]=0; } for(int i=2;i<=n;i++) { for(int j=0;j<=n;j++) for(int k=0;k+j<=n;k+=i) c2[j+k]+=c1[j]; for(int j=0;j<=n;j++) { c1[j]=c2[j]; c2[j]=0; } } return c1[n]; } public static void main(String[] args) { Scanner in=new Scanner(System.in); int n; while(in.hasNext()) { n=in.nextInt(); System.out.println(cal(n)); } } }
dp:
dp[i][j]表示i这个数划分为最大加数不超过j的划分数。
if(i>j) dp[i][j]=dp[i][j-1]+dp[i-j][j];
else if(i==j) dp[i][j]=dp[i][j-1]+1;
else if(i<j) dp[i][j]=dp[i][i];
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; int dp[150][150]; int main() { int n,m; //dp[1][1]=1; for(int i=1; i<=150; i++) for(int j=1; j<=150; j++) { if(j==1) dp[i][j]=1; else if(i==j) dp[i][j]=dp[i][j-1]+1; else if(i>j) dp[i][j]=dp[i][j-1]+dp[i-j][j]; else if(i<j) dp[i][j]=dp[i][i]; } while(scanf("%d",&n)!=EOF) { printf("%d\n",dp[n][n]); } return 0; }
HDU_1028_Ignatius and the Princess III_(母函数,dp)
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5534458.html