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. IP地址资源的分配和管理

    IP地址资源的分配和管理 参考资料 https://wenku.baidu.com/view/3bdf94172cc58bd63086bd8c.html   http://www.iana.org/ ...

  2. eclipse Java EE安装和web项目的创建

    一.根据http://www.itnose.net/detail/6139800.html基本安装成功二.根据http://www.cnblogs.com/freebsd-pann/archive/2 ...

  3. NOJ——1627Alex’s Game(II)(尺取)

    [1627] Alex’s Game(II) 时间限制: 2000 ms 内存限制: 65535 K 问题描述 Alex likes to play with one and zero as you ...

  4. iOS-Http : GET : POST

    一.概述 * HTTP/1.1协议共定义了8中请求方法:OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT. * GET方法和POST是我们使用 ...

  5. NIO系列1:框架拆解

    最近一年用NIO写了不少网络程序,也研究了一些开源NIO网络框架netty.mina等,总结了一下NIO的架构特点. 无论是netty还是mina它们都在java原生NIO的基础上进行了完善的封装,虽 ...

  6. Device Tree Usage 【转】

    转自:http://blog.chinaunix.net/uid-20522771-id-3457184.html 原文链接:http://devicetree.org/Device_Tree_Usa ...

  7. python print 在命令行打印带颜色

    红色 :print "\033[1;31m%s\033[0m" %("ALY : %s" %(['a','b']))

  8. Linux 之 LNMP服务器搭建-PHP

    LNMP服务器搭建-PHP 参考教程:[千峰教育] 安装: (1)解压源码包 cd /lnmp/srctar -jxvf php-7.3.2.tar.bz2cd php-7.3.2 (2)配置选项 . ...

  9. Linux 之 LNMP服务器搭建-MySQL

    LNMP服务器搭建-MySQL 参考教程:[千峰教育] 系统版本: CentOS 6.8 关闭防火墙和Selinux service iptables stop setenforce 0 安装mysq ...

  10. C语言集锦(二) 图像显示 Windows和Linux

    关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库. 了解最原始的方式,对 ...