母函数 HDU 1085 Holding Bin-Laden Captive!
这几天看了某大神写的关于母函数的文章,总算基本搞明白,就找这类题练练,这题是母函数变种
一开始WA了好几次,搞不懂原因, 后来发现原来我忽略了,不能最小的不能组合数 可以大于所有1,2,5的总和
#include <iostream> using namespace std; int main() { int n; int i, j, k; int addnum; int num[3]; int a[8005]; int b[8005]; int firstboundary; int secondboundary; bool isfind = false; //freopen("C:\\Users\\Haojian\\Desktop\\test.txt", "r", stdin); cin >> num[0] >> num[1] >> num[2]; while ( (num[0] || num[1] || num[2])) { isfind = false; n = num[0] + num[1] * 2 + num[2] * 5; //各种初始化 for (i = 0; i <= n; i++) { a[i] = 0; b[i] = 0; } for (i = 0; i <= num[0]; i++) a[i] = 1; for ( i = 2; i <= num[1] * 2; i += 2) a[i] = 1; for ( i = 5; i <= num[2] * 5; i += 5) a[i] = 1; //母函数模板 for (i = 2; i <= 3; i++) //表示第i个多项式 { //设置每次的边界,和k加的次数 if (i == 3) { addnum = 5; firstboundary = num[0] + num[1] * 2 ; secondboundary = num[0] + num[1] * 2 + num[2] * 5; } else { addnum = 2; firstboundary = num[0]; secondboundary = num[0] + num[1]*2; } //多项式计算 for (j = 0; j <= firstboundary; j++) { for (k = 0; k + j <= secondboundary; k += addnum)//第i个多项式的每个项 b[j + k] += a[j]; } for (k = 0; k <= secondboundary; k++) { a[k] = b[k]; b[k] = 0; } } //查找答案输出 for (i = 1; i <=n; i++) { if (a[i] == 0) { isfind = true; cout << i << endl; break; } } //如果查找不到,意味着最小的不可能组合的数就为总数+1 if (!isfind) cout << n+1 << endl; cin >> num[0] >> num[1] >> num[2]; } return 0; }