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

http://www.lydsy.com/JudgeOnline/problem.php?id=1655

背包就没什么好说的了,裸的完全背包。。

但是我一开始交开了ull都wa了T_T。。

精度太大。。。

。。

打高精度吧。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int M=1005;
int n, k;
int f[M][M];

void add(int a[], int b[]) {
	int lena=a[0], lenb=b[0], lenc=max(lena, lenb);
	int k=0, i;
	for(i=1; i<=lenc || k; ++i) {
		k=(a[i]+b[i])/10;
		a[i]+=b[i]; a[i]%=10;
		a[i+1]+=k;
	}
	a[0]=i-1;
}

int main() {
	read(n); read(k);
	f[0][1]=f[0][0]=1;
	for1(i, 1, k) {
		for1(j, i, n)
			add(f[j], f[j-i]);
	}
	int t=f[n][0];
	for3(i, t, 1) print(f[n][i]);
	return 0;
}

 

 


 

 

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

    约翰到奶牛商场里买工具.商场里有K(1≤K≤100).种工具,价格分别为1,2,…,K美元.约翰手里有N(1≤N≤1000)美元,必须花完.那他有多少种购买的组合呢?

Input

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

    仅一行,输入N,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

HINT

Source

posted @ 2014-09-10 12:57  iwtwiioi  阅读(359)  评论(0编辑  收藏  举报