poj 3181 Dollar Dayz (完全背包求方案数+高精度)

题意:Farmer John 想知道,在一家商品价格为1至K (1 <= K <= 100)的店有多少方法能正好花完他的N(1 <= N <= 1000)元钱 。商品数量充足。

思路:和前面写的完全背包求方案数代码很像,只是这题的最大数据很大,会爆 long long ,因此要注意处理.
这里最常用的处理方法就是使用两个long long 数组来处理存储这个结果,一个存高位,一个存低位,这样子,就基本上将可以表达的范围1e18扩大到了1e36.

#include <iostream>
#include  <cstdio>
#include<algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
ll a[1004];//dp[i]表示花i元在价格最多为k的店中的方法数
ll b[1004];
const ll mod = 1e18;
int n, k;
int main() {
    scanf("%d%d", &n, &k);
    b[0] = 1;
    for(int i = 1; i <= k; i++) {
        for(int j = i; j <= n; j++) {
            a[j] = (b[j] + b[j - i]) / mod + a[j] + a[j - i];
            b[j] = (b[j] + b[j - i]) % mod;
        }
    }
    if(a[n] == 0) printf("%lld\n", b[n]);
    else printf("%lld%lld\n",a[n], b[n]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/80464263
今日推荐