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

题意:给你两个数n。k,让你用1到k这k个数表示n,问有几种方法,本质是整数的拆分。由于最后结果比較大,超过long long ,所以用两个long long连接起来,设两个数组a[][],b[][]分别表示没有超过long long的部分以及超过long long 部分。

当中a[n][m]表示n用一些数拆分,当中最大的数不超过m的方案数,绘图能够看到。当n<m时,a[i][j]=a[i][i];当n>=m时。a[i][j]=(a[i][j-1]+a[i-j][j])%inf;初始化的时候要令a[0][i]=1,由于a[2][2]=a[0][2]+a[2][1];

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

#include<stdio.h>
#include<string.h>
#define ll long long
ll a[1006][106],b[1006][106];
ll inf;
int main()
{
int n,m,i,j;
inf=1;
for(i=1;i<=18;i++){
inf*=10;
}
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=1;i<=m;i++){
a[0][i]=1;
}
for(i=1;i<=n;i++){
a[i][1]=1;
}
for(j=1;j<=m;j++){
a[1][j]=1;
} for(j=2;j<=m;j++){
for(i=2;i<=n;i++){
if(i<j){
a[i][j]=a[i][i];
b[i][j]=b[i][i];
}
else{
a[i][j]=(a[i][j-1]+a[i-j][j])%inf;
b[i][j]=b[i][j-1]+b[i-j][j]+(a[i][j-1]+a[i-j][j])/inf;
}
//printf("%d %d %d\n",i,j,a[i][j]);
}
}
if(b[n][m])
printf("%lld",b[n][m]);
printf("%lld\n",a[n][m]);
}
return 0;
}

这题也能够用全然背包,并用高精度模拟。状态转移方程:dp[j]+=dp[j-w[i]]

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define inf 0x7fffffff
#define ll long long
int dp[1005][60];
void add(int a,int b){
int i,j;
for(i=1;i<=50;i++){
if(dp[a][i]+dp[b][i]<=9){
dp[a][i]=dp[a][i]+dp[b][i];
}
else{
dp[a][i]=(dp[a][i]+dp[b][i])%10;
dp[a][i+1]++;
}
}
} int main()
{
int n,m,i,j,k,t;
while(scanf("%d%d",&m,&k)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0][1]=1;
for(i=1;i<=k;i++){
for(j=i;j<=m;j++){
add(j,j-i);
}
}
t=50;
while(t>=2 && dp[m][t]==0)t--; for(i=t;i>=1;i--){
printf("%d",dp[m][i]);
}
printf("\n");
}
return 0;
}

poj3181 Dollar Dayz的更多相关文章

  1. poj3181 Dollar Dayz ——完全背包

    link:http://poj.org/problem?id=3181 本来很常规的一道完全背包,比较有意思的一点是,结果会超int,更有意思的解决方法是,不用高精度,用两个整型的拼接起来就行了.OR ...

  2. Dollar Dayz(大数母函数,高低位存取)

    Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5655   Accepted: 2125 Descr ...

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

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  4. poj 3181 Dollar Dayz(完全背包)

    Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5419   Accepted: 2054 Descr ...

  5. Dollar Dayz poj3181

    http://poj.org/problem?id=3181 这个题目一开始就能看出来是个dp问题,但是我并没有一开始就看出来是一个完全背包为题,只是想着根据以前的方法,这个问题应该是可以找到规律的, ...

  6. poj3181【Dollar Dayz】

    做完这道题,心里五味陈杂,明明是最水的一道题,我却做了最长的时间. 题意是求用1-k的和表示n的方案数. 显然是个计数dp,但我不会.思考半小时未果. 然后找尹鹏哲,他给我讲了个错的dp方程,结果调试 ...

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

    http://poj.org/problem?id=3181 Description Farmer John goes to Dollar Days at The Cow Store and disc ...

  8. bzoj1655 [Usaco2006 Jan] Dollar Dayz 奶牛商店

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...

  9. 【BZOJ】1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(背包+高精度)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1655 背包就没什么好说的了,裸的完全背包.. 但是我一开始交开了ull都wa了T_T.. 精度太大. ...

随机推荐

  1. 【bzoj3676】[Apio2014]回文串 回文自动机

    题目描述 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最大出现值. 输入 输入只有一行,为一个只包含小写字母( ...

  2. ACM程序设计选修课——1043: Radical loves integer sequences(YY)

    1043: Radical loves integer sequences Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 36  Solved: 4 ...

  3. WIFI万能钥匙协议分析

    WIFI万能钥匙协议分析 需求: 上android 市场下载任意一款,wifi万能钥匙 软件,对其进行 协议分析和逆向,达成如下结果:通过对软件的分析,完成自动化爬虫,爬wifi万能钥匙的wifi库, ...

  4. Static相关

    [理解] 说到static,脑中浮现的几个Key Words是什么? main 类 唯一空间 所有对象共享 static只能处理static 很好,解释一下上面的意思: main static fie ...

  5. Java面试题之什么情况下会触发类的初始化

    以下情况会触发类的初始化: 遇到new,getstatic,putstatic,invokestatic这4条指令: 使用java.lang.reflect包的方法对类进行反射调用: 初始化一个类的时 ...

  6. python 考试补缺

    十六进制: >>> a=100 >>> hex(a) '0x64' 八进制: >>> a=100 >>> oct(a) '0o1 ...

  7. fullpage在vue单页面当中使用会出现的问题以及解决办法

    在 vue 单页面当中发现fullpage会报错,报错信息大概意思为,fullpage不允许初始化多次. 解决办法,在使用fullpage的组件跳转路由进入销毁组件之前的生命周期的时候对fullpag ...

  8. 配置vscode使它能够在自定义扩展名当中支持emment语法

    在.vue文件当中默认是不支持emment的,需要在vscode设置当中设置 "emmet.syntaxProfiles": { "vue-html": &qu ...

  9. Gcd(bzoj 2818)

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...

  10. 2017.8.1 Noip2018模拟测试赛(十七)

    日期: 八月第一天  总分: 300分  难度: 提高 ~ 省选    得分: 100分(不应该啊!) 题目目录: T1:战争调度 T2:选数 T3:由乃的OJ 赛后心得: MMP,首先第一题花了大概 ...