[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 ...
随机推荐
- k64 datasheet学习笔记1---概述
1.前言 k64 datasheet描述了Freescale MCU的特性.架构和编程模型,主要是面向使用MCU的系统架构和软件应用开发人员. 2.模块划分 datasheet主要按功能对模块进行划分 ...
- Linux下rsyslog日志收集服务环境部署记录【转】
rsyslog 可以理解为多线程增强版的syslog. 在syslog的基础上扩展了很多其他功能,如数据库支持(MySQL.PostgreSQL.Oracle等).日志内容筛选.定义日志格式模板等.目 ...
- openstack swift节点安装手册3-最后的安装配置及验证
以下步骤都在controller节点上执行 1.远程获取/etc/swift/swift.conf文件: curl -o /etc/swift/swift.conf https://git.opens ...
- 带你玩转Visual Studio——带你理解微软的预编译头技术
原文地址:http://blog.csdn.net/luoweifu/article/details/49010627 不陌生的stdafx.h 还记得带你玩转Visual Studio——带你新建一 ...
- KVM -> 虚拟机在线热添加技术_04
热添加技术 1.KVM在线热添加硬盘
- 转载:分布式文件系统 - FastDFS 在 CentOS 下配置安装部署(2)
原文:http://blog.mayongfa.cn/193.html 一.安装 Nginx 和 fastdfs-nginx-module 安装 Nginx 请看:从零开始学 Java - CentO ...
- SQL代码整理
--SQL代码整理: create database mingzi--创建数据库go--连接符(可省略)create table biao--创建表( lieming1 int not null,-- ...
- gt_argmax_overlaps = overlaps.argmax(axis=0) ValueError: attempt to get argmax of an empty sequence错误处理
在faster rcnn内进行随机裁剪数据增强,训练一段时间后报错: gt_argmax_overlaps = overlaps.argmax(axis=0) ValueError: attempt ...
- poj12482 扫描线+lazy-tag
采用扫描线的思想,其实是区间更新的题目 题解链接https://blog.csdn.net/shiqi_614/article/details/7819232 注意处理细节:1)因为边框上的点不算,所 ...
- linux常用软件安装,常用命令
jdk [root@localhost]# tar -zxvf jdk-8u144-linux-x64.tar.gz [root@localhost]# vi /etc/profile 在profil ...