poj3181 Dollar Dayz ——完全背包

August 15, 2013
POJ DP

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
typedef  unsigned long long ULL;
ULL dp[1111], dp1[1111];
int main(void) {
    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r",stdin);
#endif
    ULL tmp = 1;
    for (int i = 1; i < 19; ++i) tmp*=10;
    int n, k;
    while (cin>>n>>k) {
        memset(dp,0,sizeof(dp));
        memset(dp1,0,sizeof(dp1));
        dp[0] = 1;
        for (int i = 1; i <= k; ++i) {
            for (int j=i; j<= n; ++j) {
                dp1[j] += (dp1[j-i] + (dp[j]+dp[j-i])/tmp);
                dp[j] = (dp[j]+dp[j-i])%tmp;
            }
        }
        if (dp1[n]) cout<<dp1[n];
        cout<<dp[n]<<endl;
    }
    return 0;
}

o(╯□╰)o

comments powered by Disqus