hdu1028 Ignatius and the Princess III

Title link

solution

There are two solutions to this problem.

The first solution is a naked full backpack.

It is equivalent to n items, and the weight of the i-th item is i. There are an infinite number of each item, and ask the number of solutions that happen to fill a backpack with capacity n.

The second solution is to generate functions.

The generator function \ ((1 + x + x ^ 1 + x ^ 2 + ...) \) represents the number of \ (1 \) split . Use \ ((1 + x ^ 2 + x ^ 4 + x ^ 6 + ...) \) to denote the number of \ (2 \) split . The rest is the same. The final \ (x ^ n \) coefficient is the answer.

code

Solution 1

/*
* @Author: wxyww
* @Date:   2020-04-16 14:18:39
* @Last Modified time: 2020-04-16 14:19:55
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;

ll read() {
	ll x = 0,f = 1;char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1; c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0'; c = getchar();
	}
	return x * f;
}
int f[150];
int main() {
	int n;
	while(~scanf("%d",&n)) {
		memset(f,0,sizeof(f));
		f[0] = 1;
		for(int i = 1;i <= n;++i) {
			for(int j = i;j <= n;++j) 
				f[j] += f[j - i];
		}
		cout<<f[n]<<endl;
	}

	return 0;
}

Solution 2

/*
* @Author: wxyww
* @Date:   2020-04-16 14:11:40
* @Last Modified time: 2020-04-16 14:19:27
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 150;
ll read() {
	ll x = 0,f = 1;char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1; c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0'; c = getchar();
	}
	return x * f;
}
int f[N][N],tmp[N];
int main() {
	int n;
	while(~scanf("%d",&n)) {
		memset(f,0,sizeof(f));
		for(int i = 0;i <= n;++i) f[1][i] = 1;
		for(int t = 2;t <= n;++t) {
			for(int i = 0;i <= n;++i) {
				for(int k = 0;k + i <= n;k += t) {
					f[t][i + k] += f[t - 1][i];
				}
			}
		}
		cout<<f[n][n]<<endl;
	}

	return 0;
}

Guess you like

Origin www.cnblogs.com/wxyww/p/hdu1028.html