完全背包+高精度 poj3181
Dollar Dayz
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2498 | Accepted: 1022 |
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$2Write 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).
1 @ US$3 + 2 @ US$1
1 @ US$2 + 3 @ US$1
2 @ US$2 + 1 @ US$1
5 @ US$1
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,求用小于等于k的数组成n的方案数。
解题思路:可以采用dp递推的思路,也可以开一个一维数组,完全背包背一遍,由于最后结果最大可以达到10^33,超出long long的数据范围,因此需要用两个long long来保存结果,
完全背包1A,爽。。。
完全背包代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
typedef __int64 ll;
struct node
{
ll a,b;
bool flag;
};
const ll mod=100000000000000000ll;
node dp[1110];
int main()
{
ll i,j,k,m,n,p,q;
while(~scanf("%I64d%I64d",&n,&k))
{
for(i=0;i<=1100;i++)
{
dp[i].a=0;
dp[i].b=0;
dp[i].flag=0;
}
dp[0].flag=1;
dp[0].b=1;
for(i=1;i<=k;i++)
for(j=0;j<=n;j++)
if(dp[j].flag)
{
dp[j+i].flag=1;
p=dp[j+i].b+dp[j].b;
dp[j+i].b=p%mod;
dp[j+i].a=dp[j+i].a+dp[j].a+p/mod;
}
if(dp[n].a==0)printf("%I64d\n",dp[n].b);
else
{
printf("%I64d",dp[n].a);
printf("%017I64d\n",dp[n].b);
}
}
return 0;
}

浙公网安备 33010602011771号