[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. 非技术1-学期总结&ending 2016

    好久好久没写博客了,感觉动力都不足了--12月只发了一篇博客,好惭愧-- 今天是2016年最后一天,怎么能不写点东西呢!! 学期总结 大学中最关键一年的第一个学期,共4个月.前20天在学网络方面的,当 ...

  2. android: Incorrect line ending: found carriage return (\r) without corresponding newline (\n)

    当报这种错误的时候:Incorrect line ending: found carriage return (\r) without corresponding newline (\n) 解决方法: ...

  3. BZOJ 3870: Our happy ending( 状压dp )

    dp(i, s)表示考虑了前i个数后, 能取到的数的集合为s时的方案数.对于1~min(L, K)枚举更新, 剩下的直接乘就好了. 复杂度O(T*K*2^N)...好像有点大, 但是可以AC.... ...

  4. How to Pronounce Ending T Clusters + Homophones — Baking!

    How to Pronounce Ending T Clusters + Homophones — Baking! Share Tweet Share Tagged With: ARE Reducti ...

  5. Beginning and Ending the Speech

    Beginning and Ending the Speech Just as musical plays need appropriate beginnings and endings, so do ...

  6. Every ending is just a new beginning.

    Every ending is just a new beginning.每次结束都是新的开始.

  7. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

  8. 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 ...

  9. 解决php - Laravel rules preg_match(): No ending delimiter '/' found 问题

    ### 说明解决php - Laravel preg_match(): No ending delimiter '/' found 一.遇到问题的原因本正常添加如下 public function r ...

随机推荐

  1. emmc基础技术8:操作模式4-data transfer mode

    1.前言 eMMC总线操作包含: boot mode, device identification mode interrupt mode data transfer mode 本文主要描述data ...

  2. pt-online-schema-change VS oak-online-alter-table

    前言 在上篇文章中提到了MySQL 5.6 Online DDL,如果是MySQL 5.5的版本在DDL方面是要付出代价的,虽然已经有了Fast index Creation,但是在添加字段还是会锁表 ...

  3. python获取当前环境的编码

    # coding:gbk import sys import locale def p(f): print '%s.%s(): %s' % (f.__module__, f.__name__, f() ...

  4. 对WinMain程序入口函数返回值为msg.wParam的几点理解

    原文地址:http://blog.csdn.net/setflvet/article/details/6983224 1.在WinMain主函数中,最后的返回值是msg.wParam,这个参数是传递给 ...

  5. P3567 [POI2014]KUR-Couriers

    题目描述 Byteasar works for the BAJ company, which sells computer games. The BAJ company cooperates with ...

  6. Vue.js——循环(Java、JSTL标签库、数据库)

    一.Vue.js循环 Vue.js循环要使用 v-for 指令. v-for 指令需要以 student in StudentList 形式的特殊语法使用, StudentList 是源数据数组并且s ...

  7. Java上传文件FTP服务器代码

    1. 在实际的应用重,通常是通过程序来进行文件的上传. 2. 实现java上传文件到ftp服务器中 新建maven项目 添加依赖 <dependency> <groupId>c ...

  8. GBDT学习

    白话GBDT: https://blog.csdn.net/qq_26598445/article/details/80853873 优点: 预测精度高 适合低维数据 能处理非线性数据,该版本GBDT ...

  9. log4j2配置文件log4j2.xml

    原地址:https://www.cnblogs.com/hafiz/p/6170702.html 1.关于配置文件的名称以及在项目中的存放位置 log4j 2.x版本不再支持像1.x中的.proper ...

  10. sql基础笔记备忘

    MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型:tinyint smallint medium ...