24h購物| | PChome| 登入
2012-10-21 12:52:32| 人氣365| 回應0 | 上一篇 | 下一篇

NCPC2012B Integer Partition

推薦 0 收藏 0 轉貼0 訂閱站台


Problem B

Integer Partition

Input File: pb.in

Time Limit: 5 seconds

 

        A new partition problem is defined as follows. Let the symbol Py,zi be the number of ways to write a positive integer y as a sum of i positive integers having the largest part no larger than z, i.e.,

y = a1+a2+…+ai, and z >= a1 >= a2 … >= ai >= 1

Notice that two sums differing only in the order of their summands are considered to be the same partition. For example, P25,4 = 2 (can be partitioned in two distinct ways: 4+1, 3+2) and P25,3­ = 1 (can be partitioned in one single way: 3+2).

        Please write a program to compute the number of Piy,z with given integers y, i, and z, which y may be as large as up to 500.

 

Technical Specification

1.    1 <= y <= 500

2.    1 <= i <= 50

3.    1 <= z <= 100

輸入說明 :

The first line of the input file contains one integer m(<= 5) indicating the number of test cases to following m lines, there are three integers y, i, and z.

輸出說明 :

For each test case, output Piy,z.

範例輸入 :

520 4 7100 50 51487 18 87492 19 89500 19 90

範例輸出 :

1320422671398241360047622043074039467989125985433353057732

提示 :

出處 :

NCPC2012 (管理:morris1028)
這題是很簡單的 dp 題, 即使使用最爛的暴力解 dp[y][i][z] 一樣可以通過, 雖然記憶體會拖累速度, 反正能過就好, 有何不可能?


#include <stdio.h>

int main() {
    int t, y, I, z;
    int i, j, k;
    scanf("%d", &t);
    while(t--) {
        long long dp[501][51] = {};
        dp[0][0] = 1;
        scanf("%d %d %d", &y, &I, &z);
        for(i = 1; i <= z; i++) {
            for(j = i; j <= y; j++) {
                for(k = 1; k <= I; k++) {
                    dp[j][k] += dp[j-i][k-1];
                }
            }
        }
        printf("%lld\n", dp[y][I]);
    }
    return 0;
}

台長: Morris
人氣(365) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 資訊競賽 |
此分類下一篇:NCPC2012F Optimal Transformation Cost
此分類上一篇:NCPC2012I Christmas Gifts

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文