dp(i, s)表示考虑了前i个数后, 能取到的数的集合为s时的方案数.对于1~min(L, K)枚举更新, 剩下的直接乘就好了. 复杂度O(T*K*2^N)。。。好像有点大, 但是可以AC。。。。

------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
typedef long long ll;
#define b(i) (1 << (i))
 
const int MOD = 1000000007;
 
int N, g, L, dp[b(21)];
 
inline void upd(int &x, int t) {
if((x += t) >= MOD) x -= MOD;
}
 
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &N, &g, &L);
memset(dp, 0, sizeof dp);
dp[1] = 1;
int All = b(g + 1) - 1;
while(N--) {
for(int s = All; s; s--) if(dp[s]) {
int t = dp[s];
for(int i = min(g, L); i; i--)
upd(dp[s | ((s << i) & All) | b(i)], t);
if(L > g) upd(dp[s], ll(t) * (L - g) % MOD);
}
}
int ans = 0;
for(int i = All; i; i--)
if(i & b(g)) upd(ans, dp[i]);
printf("%d\n", ans);
}
return 0;
}

------------------------------------------------------------------------------

3870: Our happy ending

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 146  Solved: 84
[Submit][Status][Discuss]

Description

There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.
Y*wan still remember the day he first meets the devil. Now everything is done and the devil is gone. Y*wan feel very sad and suicide.
You feel guilty after killing so many loli, so you suicide too.
Nobody survive in this silly story, but there is still some hope, because this is just a silly background story during one programming contest!
And the last problem is:
Given a sequence a_1,a_2,...,a_n, if we can take some of them(each a_i can only be used once), and they sum to k, then we say this sequence is a good sequence.
How many good sequence are there? Given that each a_i is an integer and 0<= a_i <= L.
You should output the result modulo 10^9+7.

Input

The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains 3 integers n, k, L.
T<=20, n,k<=20 , 0<=L<=10^9.

Output

For each cases, output the answer in a single line.

Sample Input

1
2 2 2

Sample Output

6

HINT

Source

BZOJ 3870: Our happy ending( 状压dp )的更多相关文章

  1. BZOJ.4145.[AMPPZ2014]The Prices(状压DP)

    BZOJ 比较裸的状压DP. 刚开始写麻烦惹... \(f[i][s]\)表示考虑了前\(i\)家商店,所买物品状态为\(s\)的最小花费. 可以写求一遍一定去\(i\)商店的\(f[i]\)(\(f ...

  2. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

  3. our happy ending(状压dp)

    题意:给定一个n,k,l. 问有多少长度为n的序列满足选出一些数使得他们相加为k,数列中每个数都在1-l以内. Solution 正解还是很妙的. 状压dp,设dp[i][j]表示长度为i的序列,能表 ...

  4. BZOJ.3058.四叶草魔杖(Kruskal 状压DP)

    题目链接 \(2^{16}=65536\),可以想到状压DP.但是又有\(\sum A_i\neq 0\)的问题.. 但是\(2^n\)这么小,完全可以枚举所有子集找到\(\sum A_i=0\)的, ...

  5. bzoj 5299: [Cqoi2018]解锁屏幕 状压dp+二进制

    比较简单的状压 dp,令 $f[S][i]$ 表示已经经过的点集为 $S$,且最后一个访问的位置为 $i$ 的方案数. 然后随便转移一下就可以了,可以用 $lowbit$ 来优化一下枚举. code: ...

  6. BZOJ 4197: [Noi2015]寿司晚宴 状压dp+质因数分解

    挺神的一道题 ~ 由于两个人选的数字不能有互质的情况,所以说对于一个质因子来说,如果 1 选了,则 2 不能选任何整除该质因子的数. 然后,我们发现对于 1 ~ 500 的数字来说,只可能有一个大于 ...

  7. BZOJ 3864 Hero meet devil (状压DP)

    最近写状压写的有点多,什么LIS,LCSLIS,LCSLIS,LCS全都用状压写了-这道题就是一道状压LCSLCSLCS 题意 给出一个长度为n(n<=15)n(n<=15)n(n< ...

  8. bzoj 3195 奇怪的道路 状压dp

    看范围,状压没毛病 但是如果随便连的话给开1<<16,乘上n,m就爆了 所以规定转移时只向回连边 于是想状态数组:f[i][j]表示到i这里i前K位的状态为j(表示奇偶) 发现有条数限制, ...

  9. bzoj 1556: 墓地秘密【状压dp+spfa】

    显然是状压,显然不可能把所有格子压起来 仔细观察发现只有机关周围的四个格子有用以及起点,所以我们用spfa处理出这些格子两两之间的距离(注意细节--这里写挂了好几次),然后设f[s][i]为碰完的机关 ...

随机推荐

  1. 无线网破解软件|一键式破解无线网|BT17软件包下载[笔记本+软件就行]

    从新版BT17发布到现在已经有一段时间,谢谢大家的一直来的关注.现在给大家讲解一下无线网破解问题,告诉 大家如何一键式破解WPA,WPA2,AES.Tkip等加密方式以及新版BT17软件包的下载地址. ...

  2. UVA - 10098 - Generating Fast (枚举排列)

    思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ...

  3. Sublime-text 自己定义快捷键攻略

    好吧.我承认今天非常悲剧,本来上午就写好了这篇博文,公布之后,在自己的博文里怎么也找不到. 所以如今又又一次写了一份.希望大家能顶一下吧...Nothing is better than your s ...

  4. 关于MySQL的Myisam和Innodb的一些比较总结

    总结一下MySQL的Myisam和Innodb引擎的一些差别,权当复习了. 首先二者在文件构成上: Myisam会存储三个文件:.frm 存储表结构,.MYD存储表的数据,.MYI文件存储表的索引:所 ...

  5. 动画原理——脉动(膨胀缩小)&&无规则运动

    书籍名称:HTML5-Animation-with-JavaScript 书籍源码:https://github.com/lamberta/html5-animation 1.脉动是一种半径r来回反复 ...

  6. java 获取两个日期相差的毫秒数

    方法一可以使用date的getTime()方法来将当前日期格式的时间转换为毫秒数,进而相减. long systime = new Date().getTime();//当前系统时间        l ...

  7. Python核心编程读笔 2

    第三章 python基础 一.语句和语法 \n 标准的行分隔符 \ 继续上一行 ; 将两个语句连接在一行 : 分开代码块的头和体 代码块以缩进块的形式体现 python文件以模块的形式组织 二.变量赋 ...

  8. leetcode Linked List Cycle II python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. Cent OS 修改网卡配置

    进入网卡修改界面 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 描述网卡设备名称 BOOTPROTO=static 静态IP,这里一 ...

  10. Table的分割线偏移量设置 及其 UIEdgeInset详解

    -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...