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. UVA 10163 Storage Keepers(dp + 背包)

    Problem C.Storage Keepers  Background Randy Company has N (1<=N<=100) storages. Company wants ...

  2. ie8不兼容input的placeholder属性但是要实现其效果的方法

    通过学习前辈的思想,个人想法整理如下: 通过两个元素标签,仿造出placeholder的内容 使用position定位好两个元素标签 第一要隐藏两个元素标签display:none,在<!--[ ...

  3. bootstrap-js(六)弹出框

    实例 为任意元素添加一小块浮层,用于存放非主要信息. 弹出框的标题和内容的长度都是零的话将永远不会被显示出来. 初始化 由于性能的原因,工具提示和弹出框的 data 编程接口(data api)是必须 ...

  4. SQL Server 2008如何导出带数据的脚本文件

    第一步,选中需要导出脚本的数据库,右键选中 第二步,选取弹出菜单中的任务-生成脚本选项(会弹出一SQL生成脚本的向导) 第三步,在向导中点击下一步,弹出选择数据库界面(默认是自己之前选中的数据库),把 ...

  5. Spire PDF for .NET 在ASP.NET中的使用 ---- 并非那么“美好”,有些挫折!

    笔者注:看此文前,请您先看一下上一篇文章吧. 昨天的时候,我测试了一下Spire PDF在WinForm程序中的应用,可以说用起来很简单(请忽略效率问题,没有进行测试).不过在互联网如此发达的今天,适 ...

  6. ucenter 同步登录总结

    部署: discuz默认安装后就安装了uc_server应用 各应用根目录必须有uc_client文件夹. 下载ucenter程序查看范例程序 应用管理中,应用的主 URL必须指向到应用的根目录,应用 ...

  7. leetcode Combination Sum II python

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. SQLServer 中实现类似MySQL中的group_concat函数的功能

    SQLServer中没有MySQL中的group_concat函数,可以把分组的数据连接在一起. 后在网上查找,找到了可以实现此功能的方法,特此记录下. SELECT a, stuff((SELECT ...

  9. css 里层元素撑不开外层元素

    一般是,里面那层做了高度设置,如:height, overflow等 另外可以让里面元素清楚浮动,如:clear:both

  10. 初识JavaScript,感觉整个人都不好了。。。

    学习web前端的开发已经将近一个月了,开发中的三个大兄弟——“html”.“css”.“JavaScript”,小哥我已经深入接触了前两位,并与他俩建立的深厚的友谊.在编写过程中,不能说达到各位大神的 ...