Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

背包好水,高精度好坑

Posted by ksqsf at 2017-09-06 18:26:59 on Problem 3181 and last updated at 2017-09-06 18:31:56
WA了好多次,居然是因为高精度写错了。。问题本身倒很简单,设
  dp[i][j] = 用 1~i 元凑足 j 元的方法数
很明显有
  dp[i+1][j]
= \sum_{k=0}^{\lfloor \frac{j}{i+1} \rfloor} dp[i][j-(i-1)*k]
= dp[i+1][j-i-1] + dp[i][j]

代码写得很烂,仅供参考!

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int maxk = 100;
const int maxn = 1000;
const long long base = 1000000000000000000LL;

struct BigInt {
	unsigned long long digits[2];

	BigInt() {
		digits[0] = digits[1] = 0;
	}

	BigInt operator+(const BigInt &rhs) const {
		BigInt result;

		unsigned long long carry = (digits[0] + rhs.digits[1]) / base;
		for (unsigned i = 0; i < 2; ++i) {
			unsigned long long a = digits[i];
			unsigned long long b = rhs.digits[i];
			result.digits[i] = (a + b + carry) % base;
			carry = (a + b + carry) / base;
		}
		return result;
	}

	BigInt& operator=(int i) {
		digits[1] = 0;
		digits[0] = i;
		return *this;
	}

	BigInt& operator=(const BigInt& rhs) {
		digits[0] = rhs.digits[0];
		digits[1] = rhs.digits[1];
		return *this;
	}
} dp[maxn+10];

ostream& operator<<(ostream& os, const BigInt& bi) {
	if (bi.digits[1]) {
		os << bi.digits[1];
		char buf[200];
		sprintf(buf, "%018llu", bi.digits[0]);
		for (unsigned j = 0; j < strlen(buf); j++) os << buf[j];
	} else os << bi.digits[0];
	return os;
}

int main() {
	int N, K;
	while (cin >> N >> K) {
		dp[0] = 1;
		for (int i = 0; i < K; ++i) {
			for (int j = 0; j <= N; ++j) {
				if (j >= i+1) dp[j] = dp[j] + dp[j-i-1];
			}
		}
		cout << dp[N] << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator