link:http://poj.org/problem?id=3181
本来很常规的一道完全背包,比较有意思的一点是,结果会超int,更有意思的解决方法是,不用高精度,用两个整型的拼接起来就行了。ORZ
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <queue> 9 #include <deque> 10 #include <queue> 11 #include <list> 12 #include <map> 13 #include <set> 14 #include <vector> 15 #include <utility> 16 #include <functional> 17 #include <fstream> 18 #include <iomanip> 19 #include <sstream> 20 #include <numeric> 21 #include <cassert> 22 #include <ctime> 23 #include <iterator> 24 const int INF = 0x3f3f3f3f; 25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; 26 using namespace std; 27 typedef unsigned long long ULL; 28 ULL dp[1111], dp1[1111]; 29 int main(void) { 30 ios::sync_with_stdio(false); 31 #ifndef ONLINE_JUDGE 32 freopen("in.txt", "r",stdin); 33 #endif 34 ULL tmp = 1; 35 for (int i = 1; i < 19; ++i) tmp*=10; 36 int n, k; 37 while (cin>>n>>k) { 38 memset(dp,0,sizeof(dp)); 39 memset(dp1,0,sizeof(dp1)); 40 dp[0] = 1; 41 for (int i = 1; i <= k; ++i) { 42 for (int j=i; j<= n; ++j) { 43 dp1[j] += (dp1[j-i] + (dp[j]+dp[j-i])/tmp); 44 dp[j] = (dp[j]+dp[j-i])%tmp; 45 } 46 } 47 if (dp1[n]) cout<<dp1[n]; 48 cout<<dp[n]<<endl; 49 } 50 return 0; 51 }
o(╯□╰)o