POJ3181——DP(找钱3)——Dollar Dayz

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大意:仍然是找钱问题,不过用原来的方法发现数用unsigned long long 都存不下。。只能看成完全背包来做,分成两块以下来自ACM荣耀大神 传送门

整数划分是把一个正整数 N 拆分成一组数相加并且等于 N 的问题.
比如:
6
5 + 1 (序列)
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1

假设F(N,M) 整数 N 的划分个数,其中 M 表示将 N 拆分后的序列中最大数

考虑边界状态:
M = 1 或者 N = 1 只有一个划分 既: F(1,1) = 1
M = N : 等于把M - 1 的划分数加 1 既: F(N,N) = F(N,N-1) + 1 
M > N: 按理说,N 划分后的序列中最大数是不会超过 N 的,所以 F(N,M ) = F(N,N)
M < N: 这个是最常见的, 他应该是序列中最大数为 M-1 的划分和 N-M 的划分之和, 比如F(6,4),上面例子第三行, 他应该等于对整数 3 的划分, 然后加上 2 的划分(6-4) 所以 F(N,M) = F(N, M-1) + F(N-M,M)

用动态规划来表示

dp[n][m]= dp[n][m-1]+ dp[n-m][m]
           
           dp[n][m]表示整数 n 的划分中,每个数不大于 m 的划分数。
           则划分数可以分为两种情况:
 
           a. 划分中每个数都小于 m, 相当于每个数不大于 m- 1, 故
              划分数为 dp[n][m-1].
 
           b. 划分中有一个数为 m. 那就在 n中减去 m , 剩下的就相当
              于把 n-m 进行划分, 故划分数为 dp[n-m][m];


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long  a[1005][105],b[1005][105];
long long inf = 1e18;
int main()
{
    int n,m,i,j,k;
    while(~scanf("%d%d",&n,&m)){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i = 0 ; i <= m ; i++)
            a[0][i] = 1;
        for(int j = 1; j <= m ; j++){
            for(int i= 1; i <= n; i++){
                if(i < j){
                    a[i][j] = a[i][j-1];
                    b[i][j] = b[i][j-1];
                  continue;
                }
                b[i][j] = b[i][j-1] + b[i-j][j] + (a[i][j-1] + a[i-j][j])/inf;
                a[i][j] = (a[i][j-1] + a[i-j][j])%inf;
            }
        }
        if(b[n][m])
            printf("%lld",b[n][m]);
        printf("%lld\n",a[n][m]);
    }
return 0;
}


 
时间: 2024-10-25 06:41:18

POJ3181——DP(找钱3)——Dollar Dayz的相关文章

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 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

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

[POJ3181] Dollar Dayz

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

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 poj3181

http://poj.org/problem?id=3181 这个题目一开始就能看出来是个dp问题,但是我并没有一开始就看出来是一个完全背包为题,只是想着根据以前的方法,这个问题应该是可以找到规律的,但是,还是被坑了,这还是一个大数问题! 首先我膜拜一下hankcs大神的: /////////////////////////////////////////////////////////// #include <iostream> using namespace std; unsigned l

poj 3181 Dollar Dayz DP

题意:给你一个n,还有k,求问有多少种数字组合,能够使得数字之和为n,这些数字的范围是1到k. 如,给你n=4, k=2.那么 1+1+1+1=4, 1+1+2=4,2+2=4,四种组合. 思路:完全背包,可以设d[i][j]代表从i个数字相加和为j的组合数. 那么,可以考虑把这些组合数分为,有数字i和没有数字i,那么没有数字i的组合数就为d[i-1][j],有数字i的组合数就为d[i][j-i](可以在这些组合里面加上1个i). 所以,转移方程可以写成d[i][j] = d[i-1][j] +

Dollar Dayz 【完全背包+高精度】

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