[HDU4906]Our happy ending
[HDU4906]Our happy ending
题目大意:
让你构造一个\(n(n\le20)\)个数的数列,其中每个数都为小于等于\(l(l\le10^9)\)的非负整数。
问你能构造出多少个这样的数列,使其其中几个数相加和为\(k(k\le20)\)。
思路:
状压DP。
\(f[i][j]\)表示有\(i\)个数在\(0\sim \min(k,l)\)范围内,可以构成的和的集合为\(j\)的时,这\(i\)个数取值的方案数。
计算答案时乘以\(i\)是哪些位置的组合数,以及剩下\(n-i\)个位置填什么的方案数即可。
时间复杂度\(\mathcal O(2^nn(\log(n)+k))\)。
源代码:
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=21,K=21,mod=1e9+7;
int f[2][1<<K],fac[N],ifac[N];
void exgcd(const int &a,const int &b,int &x,int &y) {
if(!b) {
x=1,y=0;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline int inv(const int &x) {
int ret,tmp;
exgcd(x,mod,ret,tmp);
return (ret%mod+mod)%mod;
}
inline int C(const int &n,const int &m) {
return (int64)fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
inline int power(int a,int k) {
int ret=1;
for(;k;k>>=1) {
if(k&1) ret=(int64)ret*a%mod;
a=(int64)a*a%mod;
}
return ret;
}
int main() {
for(register int T=getint();T;T--) {
const int n=getint(),k=getint(),l=getint();
for(register int i=fac[0]=1;i<=n;i++) {
fac[i]=(int64)fac[i-1]*i%mod;
}
ifac[n]=inv(fac[n]);
for(register int i=n;i>=1;i--) {
ifac[i-1]=(int64)ifac[i]*i%mod;
}
const int all=(1<<(k+1))-1;
memset(f[0],0,sizeof f[0]);
f[0][1]=1;
int ans=0;
for(register int i=0;i<n;i++) {
const bool cur=i&1;
memset(f[!cur],0,sizeof f[!cur]);
for(register int j=0;j<=all;j++) {
if(f[cur][j]==0) continue;
for(register int m=0;m<=std::min(l,k);m++) {
(f[!cur][j|((j&(all>>m))<<m)]+=f[cur][j])%=mod;
}
if((j>>k)&1) {
(ans+=(int64)f[cur][j]*power(std::max(l-k,0),n-i)%mod*C(n,i)%mod)%=mod;
}
}
}
for(register int j=0;j<=all;j++) {
if((j>>k)&1) (ans+=f[n&1][j])%=mod;
}
printf("%d\n",ans);
}
return 0;
}
[HDU4906]Our happy ending的更多相关文章
- 非技术1-学期总结&ending 2016
好久好久没写博客了,感觉动力都不足了--12月只发了一篇博客,好惭愧-- 今天是2016年最后一天,怎么能不写点东西呢!! 学期总结 大学中最关键一年的第一个学期,共4个月.前20天在学网络方面的,当 ...
- android: Incorrect line ending: found carriage return (\r) without corresponding newline (\n)
当报这种错误的时候:Incorrect line ending: found carriage return (\r) without corresponding newline (\n) 解决方法: ...
- BZOJ 3870: Our happy ending( 状压dp )
dp(i, s)表示考虑了前i个数后, 能取到的数的集合为s时的方案数.对于1~min(L, K)枚举更新, 剩下的直接乘就好了. 复杂度O(T*K*2^N)...好像有点大, 但是可以AC.... ...
- How to Pronounce Ending T Clusters + Homophones — Baking!
How to Pronounce Ending T Clusters + Homophones — Baking! Share Tweet Share Tagged With: ARE Reducti ...
- Beginning and Ending the Speech
Beginning and Ending the Speech Just as musical plays need appropriate beginnings and endings, so do ...
- Every ending is just a new beginning.
Every ending is just a new beginning.每次结束都是新的开始.
- HDU 4906 Our happy ending (状压DP)
HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...
- HDU 4906 Our happy ending
题意: Given a sequence a_1,a_2,...,a_n, if we can take some of them(each a_i can only be used once), a ...
- 解决php - Laravel rules preg_match(): No ending delimiter '/' found 问题
### 说明解决php - Laravel preg_match(): No ending delimiter '/' found 一.遇到问题的原因本正常添加如下 public function r ...
随机推荐
- phantomjs 解码url
以下为部分代码: var htmlnodeInfo=(allADUrlElements.snapshotItem(i).getAttribute("href").match(/\* ...
- Python3学习笔记07-List
Python有6个序列的内置类型,但最常见的是列表和元 序列都可以进行的操作包括索引,切片,加,乘,检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法. 创建一个列表, ...
- python脚本发送邮件
#!/usr/bin/python #_*_ coding:utf-8 _*_ from email.MIMEText import MIMEText from email.MIMEMultipart ...
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
# yum install -y vim Loaded plugins: fastestmirror, presto Loading mirror speeds from cached hostfil ...
- Maven介绍及安装与配置
一.Maven的作用 在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们想到的是,难道还差 ...
- 调试Windows Service
调试Windows Service 使用一般的调试方法调试不了Windows Servers,所以参考了一些调试方法 我服务源码中使用了Timer,注意不能使用工具箱内的Timer,用System.T ...
- myeclipse2017 安装包及破解插件的下载
一:安装包下载 链接:http://pan.baidu.com/s/1miIFqha 密码:nz7h 二:操作步骤 因为工作中需要有多个MyEclipse去管理不同的项目组的工作,恰逢MyEclips ...
- 【ES】match_phrase与regexp
刚开始接触es,由于弄不清楚match_phrase和regexp导致很多查询结果与预想的不同.在这整理一下. regexp:针对的是单个词项 match_phrase:针对的是多个词项的相对位置 它 ...
- github 推送代码
一.所有更新一起推送 .git init //初始化本地仓库 . git add . //添加全部文件 .git commit -m 'add all the file' //提交修改 .git st ...
- poj3321 dfs序+树状数组单点更新 好题!
当初听郭炜老师讲时不是很懂,几个月内每次复习树状数组必看的题 树的dfs序映射在树状数组上进行单点修改,区间查询. /* 树状数组: lowbit[i] = i&-i C[i] = a[i-l ...