[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. 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码传参数 ...

  2. H5小游戏——看你有多色

    使用了封装了canvas的create.js库来实现的. 最终效果: 工程: Rect.js /* * 方块类 */ function Rect(n,color,specialColor){ crea ...

  3. Django之ModelForm(一)

    要说ModelForm,那就先说Form吧! 先给出一个Form示例: models.py from django.db import models class UserType(models.Mod ...

  4. php常用表单验证类用法实例

    <?php /** * 页面作用:常用表单验证类 * 作 者:欣然随风 * QQ:276624915 */ class class_post { //验证是否为指定长度的字母/数字组合 func ...

  5. Android稳定性测试工具Monkey的使用

    综述    Monkey是一个命令行工具,它可以运行在我们的模拟器或者设备当中.它可以发送一些伪随机的用户事件流,例如点击.触摸.手势等.我们能够使用Monkey工具来对我们所开发的应用进行压力测试. ...

  6. UIAutomation Diagram

  7. 基于scrapy的分布式爬虫抓取新浪微博个人信息和微博内容存入MySQL

    为了学习机器学习深度学习和文本挖掘方面的知识,需要获取一定的数据,新浪微博的大量数据可以作为此次研究历程的对象 一.环境准备   python 2.7  scrapy框架的部署(可以查看上一篇博客的简 ...

  8. C语言再学习之 setjmp与longjmp

    前不久在阅读Quake3源代码的时候,看到一个陌生的函数:setjmp,一番google和查询后,觉得有必要针对setjmp和longjmp这对函数写一篇blog,总结一下. setjmp和longj ...

  9. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  10. Django的自带认证系统——auth模块

    Django自带的用户认证 auth模块 from django.contrib import auth 备注:使用auth模块时,我们默认使用Django提供的auth_user表,创建数据时,可以 ...