首页 > 其他 > 详细

POJ3181 Dollar Dayz 【母函数】+【高精度】

时间:2015-01-05 15:01:27      阅读:217      评论:0      收藏:0      [点我收藏+]

Dollar Dayz
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4204   Accepted: 1635

Description

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are: 

        1 @ US$3 + 1 @ US$2

        1 @ US$3 + 2 @ US$1

        1 @ US$2 + 3 @ US$1

        2 @ US$2 + 1 @ US$1

        5 @ US$1
Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

Input

A single line with two space-separated integers: N and K.

Output

A single line with a single integer that is the number of unique ways FJ can spend his money.

Sample Input

5 3

Sample Output

5

Source


#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

#define maxn 1002
typedef long long LL;
const LL UP = 1e18;//1000000000000000000;

struct Node {
    LL u, v;
    Node(LL x = 0, LL y = 0) {
        u = x; v = y;
    }
    friend Node operator + (Node a, Node b) {
        a.u += b.u; a.v += b.v;
        if(a.v >= UP) {
            a.v -= UP; ++a.u;
        }
        return a;
    }
} c1[maxn], c2[maxn], a, b(0, 1);


int main() {
    int N, K, i, j, k;
    scanf("%d%d",&N, &K);
    c1[0] = b;
    for(i = 1; i <= K; ++i) {
        for(j = 0; j <= N; ++j)
            for(k = 0; k + j <= N; k += i)
                c2[j+k] = c2[j+k] + c1[j];
        for(j = 0; j <= N; ++j) {
            c1[j] = c2[j]; c2[j] = a;
        } 
    }
    if(c1[N].u)
        printf("%lld%017lld\n", c1[N].u, c1[N].v);
    else printf("%lld\n", c1[N].v);
    
    return 0;
}


POJ3181 Dollar Dayz 【母函数】+【高精度】

原文:http://blog.csdn.net/chang_mu/article/details/41602511

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!