题目链接

Yet another game from chef. Chef gives you N cards and M bags. Each of the N cards has an integer written on it. Now chef asks you to close your eyes and choose a subset of them. He then sums the numbers written on chosen cards, takes its absolute value and gives you those many coins. You win the game if you can divide these coins into M bags with each bag having equal share. As a first step to calculate the probability of winning, you would like to know the number of different subsets which will make you win. Note that all the cards are of different color, so even if 2 cards have the same number written on it, they are still considered as different cards.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
First line of each test case contains two integers N and QQ denotes the number of queries to be answered. Second line of each test case contains N integers, the numbers written on cards.
Following Q lines contain an integer M.

Output

For each query output the required Answer modulo 1000000009. Answer is the number of subsets that will ensure you win.

Constraints

  • 1 ≤ T ≤ 3
  • 1 ≤ N ≤ 100000
  • 1 ≤ Q ≤ 30
  • 1 ≤ M ≤ 100
  • -10^9 ≤ Number on card ≤ 10^9

Example

Input
2
5 1
1 2 -1 4 5
9
5 2
1 2 3 4 5
5
15 Output
4
8
2

Explanation

Test Case #1, Query #1
{}, {1,-1}, {1,-1,4,5}, {4,5} are winning subsets. Sums are 0, 0, 9, 9 respectively.

Test Case #2, Query #1
{}, {5}, {1,4}, {2,3}, {1,4,5}, {2,3,5}, {1,2,3,4}, {1,2,3,4,5} are winning subsets. Sums are 0, 5, 5, 5, 10, 10, 10, 15 respectively.

Test Case #2, Query #2
{}, {1,2,3,4,5} are winning subsets. Sums are 0 and 15 respectively.

Author's Note

Time Limit is not very strict (Yes, not very loose either) if correct Algorithm is used.Author's solution passes with 2 sec Time Limit (C++ solution, using scanf and printf).
Maximum Input File Size < 4MB.

题意:给出n个数,然后还有一个数字m,问有多少种方法可以从n个数中选出一些数使得这些数的和是m的倍数。

很好的一道题,学到了很多。首先对于自己的基础之差感动汗颜。

第一,对C(n, k)的打表。。。这里还是说下自己的想法吧,如果n<10^3,这个数据量基本是可以用递推的,二维数组C[n][k]记录。

像这题的n<10^5,显然无法开出巨表,所以可以使用C(n, k) = n!/(k! * (n -k)!)..这样就可以通过记录n!和n!的逆来进行求解,

如果题目给出的模数是个质数,就可以通过费马小定理很方便的求出一个数的逆元,当然扩展gcd也是可以的。

其次,对于这题的思路,看到题目和数据范围就应该想到实际上真正的数据范围就只有[0, m)...所以每次询问直接将A[i]模m,然后

得到每个数出现的次数。这样就可以得到dp的基本模型了。

dp[i][j]表示从0到i这些数中选一些数模m为j的方案数,然后dp[i][j]可以从dp[i-1][0..m-1]得到。

 Accepted Code:

 /*************************************************************************
> File Name: ANUCBC.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月04日 星期四 14时13分00秒
> Propose:
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ #define rep(i, n) for (int i = (0); i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
const int MAX_N = ;
const int MAX_M = ;
const int MOD = 1e9 + ;
typedef long long LL;
LL fact[MAX_N], ifact[MAX_N]; LL pow_mod(LL a, LL b) {
LL res = ;
while (b) {
if (b & ) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= ;
}
return res;
} //fact and ifact
void init() {
fact[] = fact[] = ifact[] = ifact[] = ;
FOR (i, , MAX_N - ) {
fact[i] = (fact[i - ] * i) % MOD;
ifact[i] = (ifact[i - ] * pow_mod(i, MOD - )) % MOD;
}
} int C(int n, int k) {
return (fact[n] * ifact[k] % MOD) * ifact[n - k] % MOD;
} int A[MAX_N], cnt[MAX_M];
LL dp[MAX_M][MAX_M], choose[MAX_M][MAX_M]; int main(void) {
init(); // precomputation ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int N, Q, M;
cin >> N >> Q;
rep (i, N) cin >> A[i];
while (Q--) {
cin >> M;
memset(cnt, , sizeof(cnt));
rep (i, N) cnt[(A[i] % M + M) % M]++; memset(choose, , sizeof(choose));
rep (i, M) FOR (j, , cnt[i]) {
choose[i][j * i % M] = (choose[i][j * i % M] + C(cnt[i], j)) % MOD;
} memset(dp, , sizeof(dp));
dp[][] = choose[][];
FOR (i, , M-) rep (j, M) rep (k, M) dp[i][j] = (dp[i][j] + dp[i - ][(j - k + M) % M] * choose[i][k]) % MOD; cout << dp[M - ][] << endl;
}
}
return ;
}

CodeChef--Cards, bags and coins的更多相关文章

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

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

  2. [CC-ANUCBC]Cards, bags and coins

    [CC-ANUCBC]Cards, bags and coins 题目大意: 给你\(n(n\le10^5)\)个数,\(q(q\le30)\)次询问,问从中选取若干个数使得这些数之和为\(m(m\l ...

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

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

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

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

  5. [CodeForce]356D Bags and Coins

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

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

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

  7. CodeChef:Little Elephant and Colored Coins

    类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...

  8. PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

    1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some ...

  9. BZOJ 1004 【HNOI2008】 Cards

    题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...

随机推荐

  1. uoj33 树上GCD

    题意:给你一棵树,根节点为1,每条边长度为1.定义f(u,v)=gcd(u-lca(u,v),lca(u,v)-v),求有多少个无序点对f(u,v)=i.对每个i输出答案. n<=20W. 标程 ...

  2. Python---求100以内的质数

    1.首先什么是质数: 一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫质数,也叫素数.如2,3,5,7,11,13,17…. 2.代码如下: 这里做个解析:①Python的for ...

  3. [JZOJ4648] 【NOIP2016提高A组模拟7.17】锦标赛

    题目 描述 题目大意 有nnn个人,你要确定一个出场序列.每次新上台的人就会和擂主打一架,胜利的人继续当擂主.题目给出两两之间打架胜利(失败)的概率. 问111选手坚持到最后的最大概率. 思考历程 看 ...

  4. [JZOJ4769]【GDOI2017模拟9.9】graph

    题目 描述 题目大意很明确了,所以不说-- 思考历程 一看见这题,咦,这就是传说中的动态图吗? 普通的动态图是维护连通性,这题是维护它是否是二分图,换言之就是维护它是否有奇环. 好像很复杂的样子. 想 ...

  5. 亲历者说:Kubernetes API 与 Operator,不为人知的开发者战争

    如果我问你,如何把一个 etcd 集群部署在 Google Cloud 或者阿里云上,你一定会不假思索的给出答案:当然是用 etcd Operator! 实际上,几乎在一夜之间,Kubernetes ...

  6. js获取base64格式图片预览上传并用php保存到本地服务器指定文件夹

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. mysql 存中文失败问题

    在向数据库存中文时报错: 将中文字段的字符集改为utf8即可: 成功截图:

  8. PAT甲级——A1085 Perfect Sequence

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...

  9. linux最基础最常用的命令快速手记 — 让手指跟上思考的速度(三)

    这一篇作为姐妹篇的第三篇,废话不多说,我觉得这个比mysql的还要重要,为什么,一旦你摊上linux 敲键盘输入命令简直是要飞的速度,不断的卡壳查命令,效率太低了,而且非常严重的影响思绪,思绪! 某些 ...

  10. UNION All中ORDER By的使用

    一个sql中,union了几个子查询.单独执行每个子查询都没问题,但union后执行,报ORA-00904: "xxx": invalid identifier关于union的使用 ...