[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. [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)

    [POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...

  2. CAD启动提示"是否关闭命令行"不管点击什么,都会闪退的解决办法

    AutoCAD splash screen starts up and then closes   AutoCAD splash screen starts up and then closes (S ...

  3. jquery 生成二维码

    jquery的二维码生成插件qrcode,在页面中调用该插件就能生成对应的二维码 <!DOCTYPE html> <html> <head> <meta ch ...

  4. Axure RP 授权码

    Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...

  5. AES,SHA1,DES,RSA,MD5区别

    AES:更快,兼容设备,安全级别高: SHA1:公钥后处理回传 DES:本地数据,安全级别低 RSA:非对称加密,有公钥和私钥 MD5:防篡改 相关: 公开密钥加密(英语:public-key cry ...

  6. 初窥ThinkPHP

    MVC全称(Model View Controller) Model:模型(可以理解位数据库操作模型) View:视图(视图显示) Controller:(控制器) 简单的说框架就是一个类的集合.集合 ...

  7. ip_rcv && ip_rcv_finish

    (1) 在inet_init中注册了类型为ETH_P_IP协议的数据包的回调ip_rcv (2) 当二层数据包接收完毕,会调用netif_receive_skb根据协议进行向上层分发 (3) 类型为E ...

  8. ubuntu下中文输入法的配置,建议用fcitx

    Fcitx [ˈfaɪtɪks] 是一个支持扩展的输入法框架.它有自己维护的三个输入法,拼音,区位和码表:还支持其他引擎,rime 中州韵,google-pinyin,sunpinyin.Fcitx ...

  9. 做php网站后台开发,在Linux系统上进行更好吗?【转载】

    1. PHP是开源软件,它在bsd/linux/win下都有很好的正式版及孪生版.并非开发php就必须要在linux下进行.主机服务商们习惯性的把asp与php分为两个主机系列几进行销售.由于asp只 ...

  10. 手游研发CJ抱大腿指南

    文摘要:CJ来了,又是一年一度的游戏圈盛事,随着手游行业的迅速崛起,今年CJ上,手游研发商以及发行商必定成为焦点.由于门槛低.市场热.前景好等因素的影响,国内一下子蹦出一大堆手游研发团队.很幸运(或者 ...