[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 ...
随机推荐
- springboot系列四、配置模板引擎、配置热部署
一.配置模板引擎 在之前所见到的信息显示发现都是以 Rest 风格进行显示,但是很明显在实际的开发之中,所有数据的显示最终都应该交由页面完成,但是这个页面并不是*.jsp 页面,而是普通的*.html ...
- [学习笔记]Javascript可选的分号
1.前言 由于Javascript有自动识别一句语句的结尾,但是缺少必要分号作为结尾符,会降低代码的可读性和整洁性.通过javascript权威指南By淘宝前端团队译,这分号还算比较好玩的. 2.样例 ...
- [Vue warn]: Do not use built-in or reserved HTML elements as component id: header
因为header在HTML5里面是个原生的标签,所以在开发的时候会提示错误,解决方法:修改components里面左边的header
- BZOJ3198 [Sdoi2013]spring 哈希 容斥原理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3198 题意概括 有n(1<=n<=100000)组数据,每组数据6个数. 现在问有几对 ...
- Phone List HDU1671
字典树的包含与不包含关系 #include<bits/stdc++.h> using namespace std; ][]; ]; ; bool insert1( char *word ) ...
- 6-5 巡逻机器人 uva1600
一开始按照标准bfs来写 标记为二维数组 后来按照三维数组写过了 ps大部分bfs都不会是二维数组搞定!!! 其中有一个bug弄了半个小时... 一开始我是先判断!vis[x][y][v.c] ...
- Unity 精灵物体的创建 Sprite.create
参考链接:http://www.cnblogs.com/BuladMian/p/6226744.html 创建预制体精灵 优点:创建大量相同精灵,只用调用一个预制体精灵,避免了 计算机大量重复创建会导 ...
- Java中的Lambda表达式
Lambda来源于希腊字母入,发音为 /'læmdə/对高数有所了解的人都知道λ用于声明一个数学逻辑系统,表示根据XX的输入参数,会返回某个Y结果.这正是编程语言中函数(方法)的意思.因此Lambd ...
- 微信小程序倒计时组件开发
今天给大家带来微信小程序倒计时组件具体开发步骤: 先来看下最终效果: git源:http://git.oschina.net/dotton/CountDown 分步骤-性子急的朋友,可以直接看最后那段 ...
- POJ - 1266 -
题目大意:给出一条圆弧上的两个端点A,B,和圆弧上两端点之间的一个点C,现在要用一块各个定点的坐标均为整数的矩形去覆盖这个圆弧,要求最小的矩形面积. 思路:叉积在本体发挥很强大的作用.首先求出三个点所 ...