UVa 10313 Pay the Price(类似数字分解DP)
题意:
把一个整数i拆分成不大于j的数字组合。
思路:
f[i, j]表示i可以拆分成多少个不大于j的数字组合。
1. 包括j : f[i, j] += f[i-j, j-1];
2. 不包括j : f[i, j] += f[i-j, j-1];
可以收缩为一维数组,具体见代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
long long int f[310];
int n, a, b;
char str[100];
void solve()
{
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 1; i <= b; ++i)
for (int j = i; j <= n; ++j)
f[j] += f[j-i];
long long int ans = f[n];
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 1; i <= a - 1; ++i)
for (int j = i; j <= n; ++j)
f[j] += f[j-i];
if (a <= 1)
printf("%lld\n", ans);
else
printf("%lld\n", ans - f[n]);
}
void init()
{
int num = sscanf(str, "%d %d %d", &n, &a, &b);
if (a > 300) a = 300;
if (b > 300) b = 300;
if (num == 1)
a = 1, b = 300;
else if (num == 2)
b = a, a = 1;
}
int main()
{
while (gets(str))
{
init();
solve();
}
return 0;
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------