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 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 Q. Q 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
2Explanation
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的更多相关文章
- CodeChef Cards, bags and coins [DP 泛型背包]
https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集.求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 100 ...
- [CC-ANUCBC]Cards, bags and coins
[CC-ANUCBC]Cards, bags and coins 题目大意: 给你\(n(n\le10^5)\)个数,\(q(q\le30)\)次询问,问从中选取若干个数使得这些数之和为\(m(m\l ...
- Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形
题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...
- Codeforces Round #207 (Div. 1) D - Bags and Coins 构造 + bitset优化dp + 分段查找优化空间
D - Bags and Coins 思路:我们可以这样构造,最大的那个肯定是作为以一个树根,所以我们只要找到一个序列a1 + a2 + a3 .... + ak 并且ak为 所有点中最大的那个,那么 ...
- [CodeForce]356D Bags and Coins
已知有n个包,和总共s个钱币. n,s<=70000. 每个包可以装钱币,还可以套别的包.每个包中的钱数等于 所有套的包的钱数 加上 自己装的钱. 所有的钱都在包内. 问给定每个包中的钱数,输出 ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
- CodeChef:Little Elephant and Colored Coins
类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...
- 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 ...
- BZOJ 1004 【HNOI2008】 Cards
题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...
随机推荐
- [转]成为Java顶尖程序员 ,看这11本书就够了
“学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远超 ...
- 五. Arrow Function 箭头函数
箭头函数三大好处: 1. 简明的语法 举例: 如果只有一个参数,可以不加(),多个参数用 "," 隔开 2. 隐式返回 首先说下什么是显示返回,显示返回就是 return 加上你要 ...
- [ javasript ] javascript中的each遍历!
1.数组中的each var arr = [ "one", "two", "three", "four"]; $.eac ...
- csps模拟68d,e,f题解
题面:https://www.cnblogs.com/Juve/articles/11655531.html 三道数据结构? d: 贪心,先按a排序,然后枚举删了前i个a值比较小的,然后在剩下的里面删 ...
- zabbix被监控端代理设置
zabbix被监控端代理设置 安装zabbix-agent客户端 rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-re ...
- cesium-长度测量和面积测量
网上找的大神的实现方法有点问题,实现有一些bug,作为cesium新手一个,弃之不忍,只好硬着头皮修改了,不过还好问题不大,再次mark一下,下次就可以直接用了 image.png import ...
- sed应用 升级场景配置文件更新 指定行追加
function addLine() { confFile=configuration.xml isExist=`cat ${confFile} | grep "<listen_ena ...
- 我学习python没有记住的东西
格式化 # 格式化 a=123 b='ww' print("%d,%s,%%"%(a,b)) # %d,%s,%f,%c,%f 格式化代码:print('{}{}'.format( ...
- Python运算符,逻辑运算
运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算,今天我们暂只学习算数运算.比较运算.逻辑运算.赋值运算 ...
- MFC中使用FLASH相关
出自http://my.oschina.net/ypimgt/blog/62573 一.准备工作 第一步:下载并安装Adobe Flash Player. 从官方网站(http://get.adobe ...