[CC-ANUCBC]Cards, bags and coins

题目大意:

给你\(n(n\le10^5)\)个数,\(q(q\le30)\)次询问,问从中选取若干个数使得这些数之和为\(m(m\le100)\)的方案数。

思路:

不难想到一个比较暴力的动态规划,用\(f[i][j]\)表示用了前\(i\)个数,和为\(j\)的方案数。时间复杂度\(\mathcal O(nmq)\)。

发现动态规划中我们只关心每个数在模\(m\)意义下的值,因此直接用\(n\)个数转移实在是太愚蠢了。

将这些数模\(m\)意义下相等的归为一类,最多有\(m\)类。直接用这\(m\)类数转移即可。

时间复杂度\(\mathcal O(qm^3)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
typedef long long int64;
const int N=2e5+1,mod=1e9+9,M=100;
int a[N],f[M],g[M],fac[N],ifac[N],cnt[M],c[M];
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;
}
int main() {
for(register int i=fac[0]=1;i<N;i++) {
fac[i]=(int64)fac[i-1]*i%mod;
}
ifac[N-1]=inv(fac[N-1]);
for(register int i=N-1;i>=1;i--) {
ifac[i-1]=(int64)ifac[i]*i%mod;
}
for(register int T=getint();T;T--) {
const int n=getint(),q=getint();
for(register int i=1;i<=n;i++) a[i]=getint();
for(register int i=0;i<q;i++) {
const int m=getint();
std::fill(&cnt[0],&cnt[m],0);
for(register int i=1;i<=n;i++) {
cnt[(a[i]%m+m)%m]++;
}
f[0]=1;
std::fill(&f[1],&f[m],0);
for(register int i=0;i<m;i++) {
std::fill(&c[0],&c[m],0);
for(register int j=0;j<=cnt[i];j++) {
(c[(int64)i*j%m]+=C(cnt[i],j))%=mod;
}
std::copy(&f[0],&f[m],g);
std::fill(&f[0],&f[m],0);
for(register int j=0;j<m;j++) {
for(register int k=0;k<m;k++) {
(f[(j+k)%m]+=(int64)g[k]*c[j]%mod)%=mod;
}
}
}
printf("%d\n",f[0]);
}
}
return 0;
}

[CC-ANUCBC]Cards, bags and coins的更多相关文章

  1. Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形

    题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...

  2. CodeChef Cards, bags and coins [DP 泛型背包]

    https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集.求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 100 ...

  3. Codeforces Round #207 (Div. 1) D - Bags and Coins 构造 + bitset优化dp + 分段查找优化空间

    D - Bags and Coins 思路:我们可以这样构造,最大的那个肯定是作为以一个树根,所以我们只要找到一个序列a1 + a2 + a3 .... + ak 并且ak为 所有点中最大的那个,那么 ...

  4. CodeChef--Cards, bags and coins

    题目链接 Yet another game from chef. Chef gives you N cards and M bags. Each of the N cards has an integ ...

  5. [CodeForce]356D Bags and Coins

    已知有n个包,和总共s个钱币. n,s<=70000. 每个包可以装钱币,还可以套别的包.每个包中的钱数等于 所有套的包的钱数 加上 自己装的钱. 所有的钱都在包内. 问给定每个包中的钱数,输出 ...

  6. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

  7. csuoj 1119: Collecting Coins

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec  Memo ...

  8. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  9. HDU 4610 Cards (合数分解,枚举)

    Cards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. mongoose使用简记

    mongodb中集合相当于表,常用指令 mongo 进入数据库 use yourdatabase 来选择你的数据集,这个跟关系型中的一样 show collections 来查看你数据集中的表,col ...

  2. 线程句柄和线程ID的区别

    ●CreateThread() API 用于创建线程. API 返回同时线程句柄,并通过参数得到线程标识符 (ID). 线程句柄有完全访问权创建线程对象. 运行线程时线程 ID 唯一标识线程在系统级别 ...

  3. 项目记录 -- config2html 理解

    html 代码: <table width=1280 border=0 cellspacing=1 cellpadding=1> <tr id=tblhdr> <td&g ...

  4. 蓝色的cms企业记账管理后台模板源码——后台

    链接:http://pan.baidu.com/s/1bpxKGBP 密码:suda

  5. WordPress404页面自定义

    不知道大家是怎么设计404页面,个性的404可以为网站增色不少,wordpress设置404是在主题里面的404.php页面上,当然比如你用Apache.nginx等服务器,你可以自己建一个单页,内容 ...

  6. java校验身份证号码

    /** * 18位身份证校验,粗略的校验 * @author lyl * @param idCard * @return */ public static boolean is18ByteIdCard ...

  7. linux下生成core dump文件方法及设置【转】

    转自:http://blog.csdn.net/mrjy1475726263/article/details/44116289 源自:http://andyniu.iteye.com/blog/196 ...

  8. Centos 6.4搭建git服务器【转】

    前阵子公司需要,让我搭个Git服务器,把之前用的SVN上代码迁移到git上去,所以就在阿里云主机上搭了一个,记录了下安装过程,留存文档以备查阅.本篇本章只涉及搭建部分的操作,更多git的使用可以参考文 ...

  9. [ python ] 字典的使用

    数据类型划分:    可变数据类型:list.dict.set    不可哈希    不可变数据类型:tuple.bool.int.str    可哈希 字典 python内置了字典类型,使用键-值( ...

  10. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...