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. SQL Server 获取服务器信息

    最近做了一个小工具,里面涉及到一些取SQL Server 服务器信息的一些东西,找了好久,找到一个不错的,贴出来分享. 系统函数 SERVERPROPERTY ( propertyname ) 包含要 ...

  2. 用事件与CSS改变按钮不同状态下的颜色

    目标效果: 表单的群发按钮,在鼠标悬停时为深蓝色,鼠标离开时为淡蓝色. HTML代码: <button id="submitBtn"  class="btn&quo ...

  3. description方法介绍及重写

  4. nutch2.3命令参数解析

    nutch中可执行的命令列表 [root@ewanalysis ~]# nutch Usage: nutch COMMAND where COMMAND is one of: inject injec ...

  5. 关于if (!cin)以及while (cin >> word)

    首先注意: 1.每一个输入(输出)对象就代表一个输入(输出)流: 2.输入(输出)对象中的流状态成员标记了输入(输出)流当前的状况,当eofbit.badbit.failbit三个标记位均为0时表示流 ...

  6. whereis linux文件搜索

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...

  7. Fragment和activity之间的通信

    1>fragment可以调用getactivity()方法获取它所在的activity. 2>activity可以调用FragmentManager的findFragmentById()或 ...

  8. jQuery中的.live()与die()

    翻译原文地址:http://www.alfajango.com/blog/exploring-jquery-live-and-die/ 很多开发者都知道jQuery的.live()方法,他们大部分知道 ...

  9. 使用javascript把图片转成base64位编码,然后传送到服务端(ajax调用的接口基于drupa7)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. hacker入门篇——相关书籍

    1.<黑客大曝光:网络安全机密与解决方案(第7版)> 简介:这是一本老外写的书,比较适合入门看,内容包括一些基本的攻防流程,基本工具软件,网络安全的一些基本概念等,对整个网络安全和黑客入侵 ...