HDU 1028-Ignatius and the Princess III(拆分整數-母函式-無限次)
Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18862 Accepted Submission(s): 13247
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!"
"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
Author
Ignatius.L
Recommend
題目意思:
給定一個正整數N,將其分解成多個正整數相加的形式。
解題思路:
母函式,套個模板,可以看作是取數無限次,不像HDU 2082 是有限次取數。
#include <bits/stdc++.h>
using namespace std;
#define maxn 150
void solve(int n)
{
int i,j,k;
int a[maxn],b[maxn];
memset(a,0,sizeof(a));//儲存各項的係數
memset(b,0,sizeof(b));//計算用中間陣列
a[0]=1;
for(i=1; i<=n; ++i)
{
for(j=0; j<=n; ++j)//列舉多項式各項
for(k=0; k*i+j<=n; ++k)
b[k*i+j]+=a[j];//每一項的係數累加
for(j=0; j<=n; ++j)
a[j]=b[j],b[j]=0;//更新各項係數,並清空中間陣列方便下一輪計算
}
cout<<a[n]<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
while(cin>>t)
{
solve(t);
}
return 0;
}
/**
4
10
20
**/
相關文章
- HDU 1028 Ignatius and the Princess III:dp or 母函式函式
- HDU 2082-找單詞(母函式-有限次)函式
- HDU 母函式簡單題 - 找單詞/Ignatius and the Princess III/Square Coins/Holding Bin-Laden Captive!函式APT
- HDU 1709 The Balance(母函式)函式
- 杭電ACM hdu 2110 Crisis of HDU 解題報告(母函式)ACM函式
- 杭電ACM hdu 1171 Big Event in HDU 解題報告(母函式)ACM函式
- 杭電ACM hdu 2152 Fruit 解題報告(母函式)ACMUI函式
- 杭電ACM hdu 1398 Square Coins 解題報告(母函式)ACM函式
- 四位整數位數拆分
- 母函式詳解和史上最通用最高效的母函式模板函式
- 杭電ACM hdu 2082 找單詞 解題報告(母函式)ACM函式
- 杭電ACM hdu 2079 選課時間 解題報告(母函式)ACM函式
- leetcode 整數拆分(c++)LeetCodeC++
- 不限次數無廣告的短網址生成工具推薦
- SG 函式初步 HDU 1536 && HDU 1944函式
- 數值得整數次方--模擬pow函式的功能函式
- 聊聊四種Oracle數字取整函式Oracle函式
- sqlserver拆分字串函式 及應用SQLServer字串函式
- HDOJ-1398 Square Coins(母函式)函式
- HDU2588GCD(尤拉函式)GC函式
- 杭電ACM hdu 1085 Holding Bin-Laden Captive! 解題報告(母函式)ACMAPT函式
- LeetCode-343. 整數拆分 - 題解分析LeetCode
- HDU1729 Stone Game (SG函式)GAM函式
- LeetCode 343. 整數拆分--動態規劃LeetCode動態規劃
- The Princess And The Pea
- 『無為則無心』Python函式 — 26、Python函式引數的傳遞方式Python函式
- HDU 4002Find the maximum(尤拉函式)函式
- (40/60)整數拆分、不同的二叉搜尋樹
- HDU 4279 2012網路賽Number(數論 尤拉函式結論約數個數)函式
- C語言atoi()函式:將字串轉換成int(整數)C語言函式字串
- 字元函式、數字函式和日期函式字元函式
- HDU-OJ-1017 A Mathematical Curiosity-整數對iOS
- 函式定義、函式的引數、函式的預設引數函式
- 【函式】Oracle函式系列(2)--數學函式及日期函式函式Oracle
- 聚合函式與數字函式函式
- 邊學邊寫——母函式及其在中學數學競賽中的運用(一)函式
- 『無為則無心』Python函式 — 33、高階函式Python函式
- 不同維度的拆分:物件導向和函式式的區別物件函式