題解報告:hdu 1028 Ignatius and the Princess III(母函數orDP)
阿新 • • 發佈:2018-05-20
函數 OS fir c++ ali 元素 namespace output 一個數 Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!" Input The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file. Output For each test case, you have to output a line contains an integer P which indicate the different equations you have found. Sample Input 4 10 20 Sample Output 5 42 627 解題思路:這題既可以用動態規劃,也可以用母函數。
DP原先是遞歸而來的,這裏就直接講遞推吧!
首先定義dp[i][j]記錄將整數i劃分成所有元素都不大於j(即小於或等於j)的所有情況數,下面舉個栗子:
當i=4,j=1時,要求劃分得到的所有元素都不大於j,所以劃分法只有1種:{1,1,1,1};
當i=4,j=2時,。。。。。。。。。。。。。。。。。。。。。只有2種:{1,1,1,1},{2,1,1},{2,2};
當i=4,j=3時,。。。。。。。。。。。。。。。。。。。。。只有3種:{1,1,1,1},{2,1,1},{2,2},{3,1};
當i=4,j=4時,。。。。。。。。。。。。。。。。。。。。。只有4種:{1,1,1,1},{2,1,1},{2,2},{3,1},{4};
當i=4,j=5,6.....n(n為自然數)時,。。。。。。。。。。只有4種:{1,1,1,1},{2,1,1},{2,2},{3,1},{4};
從以上規律可以推出結論:當i==1||j==1,只有一種劃分法;
當i<j時,由於發法不可能出現負數,所以dp[i][j]=dp[i][i];
當i==j時,分兩種情況:①如果要分出j這一個數,那麽情況只有1種:{j};②如果不分,那麽就將i分成所有元素不大於j-1的若幹份,dp[i][j]=dp[i][j-1]。所以總的情況數為dp[i][j]=dp[i][j-1]+1;
當i>j時,也分兩種情況:①如果要分出j這一個數,註意此時的j<i,說明其剩下的(i-j)劃分成的所有元素都在這個集合{j,a1 ,a2...}裏,那麽此時的dp[i][j]為把剩下的(i-j)這個整數劃分成所有元素都不大於j時的情況數,即dp[i][j]=dp[i-j][j];②如果不分,那麽就將i分成所有元素不大於j-1的若幹份,dp[i][j]=dp[i][j-1]。所以總的情況數為dp[i][j]=dp[i-j][j]+dp[i][j-1]。
AC代碼之DP:
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!" Input The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file. Output For each test case, you have to output a line contains an integer P which indicate the different equations you have found. Sample Input 4 10 20 Sample Output 5 42 627 解題思路:這題既可以用動態規劃,也可以用母函數。
AC代碼之DP:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 125; 5 LL dp[maxn][maxn];int n;//dp[i][j]記錄將整數i劃分成所有元素不大於j的所有情況數 6 int main() 7 {//打表 8 for(int i=0;i<maxn;i++)//將整數i劃分成所有元素不大於1的分法和將整數1劃分成所有元素不大於i(其實只有1本身)的分法都為1種 9 dp[i][1]=dp[1][i]=1; 10 for(int i=2;i<maxn;i++){//從2開始 11 for(int j=2;j<maxn;j++){ 12 if(i<j)dp[i][j]=dp[i][i]; 13 else if(i==j)dp[i][j]=dp[i][j-1]+1; 14 else dp[i][j]=dp[i][j-1]+dp[i-j][j]; 15 } 16 } 17 while(cin>>n){ 18 cout<<dp[n][n]<<endl;//最後輸出總的情況數,即將整數n劃分成不大於n的所有情況數 19 } 20 return 0; 21 }
題解報告:hdu 1028 Ignatius and the Princess III(母函數orDP)