(完全背包 大数)Dollar Dayz (POJ 3181)

http://poj.org/problem?id=3181

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

一看完全背包, 套了模板, 很遗憾wa了, 想了想,不明所以, 搜了下题解, 居然说是因为结果太大, 看看数据似乎是这样的, 据说是两位数存就可以, 结果写完提交,果断AC

#include <iostream>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

#define N 2100
#define met(a,b) (memset(a,b,sizeof(a)))
typedef long long LL;

LL dp[N][2];

int main()
{
    int n, m;

    while(scanf("%d%d", &n, &m)!=EOF)
    {
        int i, j;

        met(dp, 0);

        dp[0][0] = 1;
        for(i=1; i<=m; i++)
        for(j=i; j<=n; j++)
        {
            dp[j][0] += dp[j-i][0];
            dp[j][1] += dp[j-i][1];
            dp[j][1] += dp[j][0]/1000000000000000;
            dp[j][0] %= 1000000000000000;
        }

        if(dp[n][1]==0)
            printf("%I64d\n", dp[n][0]);
        else
        {
            printf("%I64d%015I64d\n", dp[n][1], dp[n][0]);
        }
    }

    return 0;
}

参考http://www.cnblogs.com/kuangbin/archive/2012/09/20/2695165.html

时间: 2024-09-28 00:37:57

(完全背包 大数)Dollar Dayz (POJ 3181)的相关文章

DP:Dollar Dayz(POJ 3181)

一道高精度DP 题目大意,换工具,有m块钱,有k种价值的物品,(1...k),求一共有多少种换法 这一题就是完全背包,现在这种完全背包对我来说就是水题了, 状态转移方程闭着眼睛写dp[j]+=dp[j-i] 可是这一题还没完,数据量太大,会出现溢出的情况,这一题有一点高精度的要求,要求也挺简单的,两个long long就可以了 状态转移方程变为: dpl[j]+=dpl[j-i]; dph[j]+=dph[j-i]+f(j)  当dpl[j]>Up时,f(j)=1,且dpl[j]=dp[j]-U

Dollar Dayz POJ - 3181

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 t

POJ 3181 Dollar Dayz(完全背包+简单高精度加法)

POJ 3181 Dollar Dayz(完全背包+简单高精度加法) http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币分别是1美元,2美元-K美元且可以无限使用,问你用上面K种硬币构成n美元的话有多少种方法? 分析: 本题是一道明显的完全背包问题, 不过本题还可以换一种方法来看: 整数n由前K个自然数构造, 一共有多少种方法? (虽然本题要用到高精度加法, 但是很简单, 不要被吓到哦) 首先是DP部分: 令dp[i][j]==x 表示由前i种硬币构成j

POJ 3181 Dollar Dayz &amp;&amp; Uva 147 Dollars(完全背包)

首先是 Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 细心看完这题后发现还是完全背包,只不过需要对浮点数处理一下.即把所有硬币的面值都乘以100,化为整数,对输入的数据也作同样的处理,然后就是套完全背包的模板了,在输出时还要用格式和精度来卡一卡你……一开始我没想到用printf可以的,于是百度了cout的输出格式控制,

POJ 3181 Dollar Dayz 【完全背包】

题意: 给出两个数,n,m,问m以内的整数有多少种组成n的方法完全背包+大数划分 思路: dp[i][j] := 用i种价格配出金额j的方案数. 那么dp[i][0] = 1,使用任何价格配出金额0的方案个数都是1(什么都不用). 递推关系式: 实际上是完全背包问题,只是状态转移方程形式有所不同,不过状态转移的方向是完全相同的. dp[i][j] = dp[i – 1][j] + dp[i – 1][j – i] + dp[i – 1][j – 2 * i] + - + dp[i – 1][0]

poj3181 Dollar Dayz (DP+大数)

Dollar Dayz Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3181 Appoint description: System Crawler (2016-05-27) Description Farmer John goes to Dollar Da

Dollar Dayz (大数dp fuck!不是多组数据!!)

Dollar Dayz Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on s

poj3181 背包+大数

http://poj.org/problem?id=3181 Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7997   Accepted: 2992 Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his

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

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 sell