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\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...
随机推荐
- server端并发聊天
mul_server和mul_client实现了客户端发什么消息,服务器端回复什么消息 server_dialog和mul_client实现了客户端与服务器并发通信
- 0906NOIP模拟测试赛后总结
我进前十辣.然而有10个大佬去学LCT了于是没有考试. Dybala神和exzkt神分-rank1,奶风神和林哥分-rank3,wc.miemeng和DuanYue神140分-rank5. 我.ooo ...
- CF1166D——数学公式思维题
#include<bits/stdc++.h> using namespace std; #define ll long long ll ans[],a,b,m; /* b=2^(n-2) ...
- 集合划分——cf1028D思维题
非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...
- win7+64位笔记本 python3.6安装opencv3
1.直接在cmd窗口下用pip,输入 pip install opencv-python 安装成功是如下界面: 不放心还可以验证下,方法是cmd窗口下输入python,然后输入 import cv2 ...
- maven错误:is duplicated in the reactor
code-instrument-java git:(masterv2-2.2.2-solr) ✗ mvn clean package -Dmaven.test.skip=true [INFO] Sca ...
- nginx部署为HTTP代理支持CONNECT模式
有个软件要走http代理,想着部署nginx起来用,结果发现用不了: 而用ccproxy的话,一切正常: 抓包分析了下,是CONNECT模式的请求 从nginx的官网http://nginx.org/ ...
- BMP 图片格式
BMP根据颜色深度,可以分为2(1位).16(4位).256(8位).65536(16位)和1670万(24位)以及32位含有alpha通道.8位图像可以是 索引彩色图像外,也可以是灰阶图像,而索引 ...
- Unity优化垃圾回收GC
- JS如何获取地址栏url后面的参数?
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:JS如何获取地址栏url后面的参数?: 这里提供了两种获取地址栏url后面参数的方法: 方式1 传参: window.location. ...