HDU 4906 Our happy ending (状压DP)
2014-07-31 23:52
411 查看
HDU 4906 Our happy ending
题目链接题意:给定n个数字,每个数字可以是0-l,要选其中一些数字,然后使得和为k,问方案
思路:状压dp,滚动数组,状态表示第i个数字,能组成的数字状态为s的状态,然后每次一个数字,循环枚举它要选取1 - min(l,k)的多少,然后进行状态转移
代码:
#include <cstdio>
#include <cstring>
typedef long long ll;
const int N = (1<<20) + 5;
const ll MOD = 1000000007;
int t, n, k;
ll l, dp
;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%lld", &n, &k, &l);
int s = (1<<k);
if (l > k) {
ll yu = l - k;
l = k;
}
memset(dp, 0, sizeof(dp));
dp[0] = 1;
while (n--) {
for (int i = s - 1; i >= 0; i--) {
if (dp[i] == 0) continue;
ll tmp = yu * dp[i] % MOD;
ll now = dp[i];
for (int j = 1; j <= l; j++) {
int next = i|((i<<j)&(s - 1)|(1<<(j - 1)));
dp[next] = (dp[next] + now) % MOD;
}
dp[i] = (dp[i] + tmp) % MOD;
}
}
ll ans = 0;
for (int i = 0; i < s; i++) {
if (i&(1<<(k - 1))) {
ans = (ans + dp[i]) % MOD;
}
}
printf("%lld\n", ans);
}
return 0;
}
相关文章推荐
- HDU 4906 Our happy ending 状压DP
- hdu 4906 Our happy ending (多校第4场 )状压DP
- hdu 4906 Our happy ending(状压dp)
- hdu 4906 Our happy ending 状压dp
- HDU 4906 Our happy ending 状压DP
- HDU 4906 Our happy ending (状压DP)
- hdu 4906 Our happy ending 状态压缩dp 较难
- hdu 4906 Our happy ending (状态压缩dp)
- hdu 4906 Our happy ending 状态压缩dp
- hdu 4906——Our happy ending
- HDU 4906 Our happy ending 解题报告(递推)
- HDU 4906 Our happy ending(2014 Multi-University Training Contest 4)
- hdu 4906 Our happy ending 。神奇的状态转移方程,记录下
- hdu 4906 状压dp
- HDU 1074 状压DP
- hdu 3001(状压dp)
- hdu 4539 郑厂长系列故事——排兵布阵 (状压DP)
- hdu 2167(状压dp)
- hdu 4284(状压dp)
- hdu1074 状压dp