1. 程式人生 > >杭電1028

杭電1028

                Ignatius and the Princess III

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

題意:就是計算例如:誰加誰等於4
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
輸出5

原理:
這裡寫圖片描述
第一項:1+x+x^2+x^3…………..
這每一項的意思:
0個1(x^0),1個1(x^1),兩個1組成2(x^2),三個1組成3…….n個1組成n(x^n)
第二項:1+x^2+x^4…………..
0個2(x^0),1個2(x^2),兩個2(x^4)…………………….

程式碼:

# include <iostream>

using namespace std;

int main(){

    int n,i,j,k;
    int c1[130],c2[130];

    while(cin>>n){

        //對第一項進行賦值 
        for(i=0;i<=n;i++){
            c1[i] = 1;
            c2[i] = 0;
        }

        for(i=2;i<=n;i++){//代表第i項 

            for(j=0;j<=n;j++){//代表i-1項,其中,滿足項的係數j<=n的進行迴圈,這裡不一定是j-1的每一項 
                for(k=0;k+j<=n;k+=i){//i項符合條件的
                    c2[j+k]+= c1[j];//i-1項的每一個符合條件的與i項相乘 
                } 
            }

            for(k=0;k<=n;k++){
                c1[k] = c2[k];//把i與i-1項的乘積從臨時儲存到c2到儲存到c1中,等待i+1項與c1的成績 
                c2[k] = 0;
            }    
        } 
        cout<<c1[n]<<endl; 
    }
    return 0;
}

相關推薦

Ignatius and the Princess III(1028)(母函數)

mission des panel mes content nat strong pro accepted Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory L

1028

Ignatius and the Princess III Problem Description “Well, it seems the first problem is too easy. I will let you

1028 整數劃分問題 母函式

c1[3] = 5 說明 x 的三次方的係數為五 說明 3 可以有5 中劃分方法 #include <iostream> using namespace std; int c1[130]

1028——整數拆分(遞迴實現)

問題描述 Problem Description “Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166

OJ 1028 整數劃分

Problem Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. "Th

1170

簡單 == while tail pan 分代 out set 初始 就是個簡單的if else判斷功能的水題目 思路 :就是讀取、計算、利用判斷語句 難點:我在做的時候判斷是否是浮點小數那邊出了點問題,因為我初始設置是double 所以輸出的時候我強制類型轉化為int 這

2018母牛的故事

處理 代碼 [1] out ++ program clas desc col program description 有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請編程實現在第n年的時候,共有多少頭母牛? input 輸入數據

1024----Max Sum Plus Plus

int oid max art main 杭電 sca ava 最大 1 /* 2 這題還沒有理解透徹。某個dalao也不寫註釋。只能自己理解了。。。 3 先求為i個元素(1<=i<=M)為一個區間的最大和,保證元素個數大於等於i個,遞推到M個即可 4

2016數列有序

break col iostream ios 空格 討論 pan include span #include<iostream>using namespace std;int main(){   int n,m,t = 0;   int a[110], b[11

2629 Identity Card

input 比較 題目 開始 ret targe str 部分 還要   題目意思很簡單,就是根據身份證號碼來確定一個人的籍貫和生日,(然而我開始腦子抽了還以為還要根據奇數偶數判斷男女233333)。   然後我的暴力ac代碼:    1 #include <io

1176 免費餡餅

sizeof 提示 () tom 一個 行為 cpp 多少 inpu http://acm.hdu.edu.cn/showproblem.php?pid=1176 免費餡餅 Time Limit: 2000/1000 MS (Java/Others) Memo

女生賽1003

%d 個數 ns2 nbsp 告訴 一句話 pan return spa 題意:給一個序列,刪除一個數,令這個序列的gcd最大 思路:求出原始序列的gcd=g,然後從左邊跑一遍gcd,如果gcd==c 標記 r,那麽a[r] 肯定和前面某個數的gcd為 c , 再從r開始完

2013 猴子吃桃 遞歸解法&循環解法

遞歸解法 algo std include algorithm ota 再次 using turn 題目估計看到過3次不止了,所以還是想復習下遞歸的運用。 奉上遞歸代碼: #include <iostream> #include<math.h> #

女生賽1001 1002 1003 1005 1008 hdu6023 6024 6025 6027 6030

node ace std ret != power tac ns2 clas 代碼先貼這裏 #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string

2023 求平均成績(及一些易見的錯誤)

競賽 斷點 sco ani 今天 new 沒有 math 的人   鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2023   首先,想說下,這題對我來說可能是一個陰影。因為在自己學校的程序競賽中,這是第二題,當時自己

2021發工資題

bsp dal 心算 name end return main spa 貪心算法 本菜雞聽dalao說這是簡單的貪心算法;加油吧菜雞 #include<iostream>using namespace std;int main(){ int n, m, an

HDU Today HDU2112【Dijkstra || SPFA】

esc 眼神 inpu mark 數據 auto 結束 性格 以及 http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 經過錦囊相助,海東集團最終度過了危機,從此。HDU的發展就

1863 暢通project

onos mon string.h ott oid 評估 iss 每一個 -s 暢通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T

HDU 4920(多校訓練#5 1010 題) Matrix multiplication(不知道該掛個什麽帽子。。。)

預處理 ica ref 循環 ring sca esp 題解 code 題目地址:HDU 4920 對這個題簡直無語到極點。。。竟然O(n^3)的復雜度能過。。。。方法有三。。 1:進行輸入優化和輸出優化。。(前提是你的輸入優化不能太搓。。。) 2:利用緩存優化。。詳情

HDU 4968(多校#9 1009題)Improving the GPA (瞎搞)

popu 推斷 imp 暴力枚舉 pop ++ pos nan sin 題目地址:HDU 4968 這題的做法是全部學科的學分情況枚舉,然後推斷在這樣的情況下是否會符合平均分。直接暴力枚舉就可以。 代碼例如以下: #include <cstring> #i