Codechef: CMDL Challenge 2014: Cards, bags and coins

Source: Internet
Author: User

Http://www.codechef.com/APRIL14/problems/ANUCBC

Statement

Yet another game from chef. Chef gives youNCards andMBags. Each ofNCards 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 has coins. you win the game if you can divide these coinsMBags with each bag having equal share. As a first step to calculate the probability of winning, you wowould like to knowNumber 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 integerTDenoting the number of test cases. The descriptionTTest cases follows.
First line of each test case contains two integersNAndQ.QDenotes the number of queries to be answered. Second line of each test case containsNIntegers, the numbers written on cards.
FollowingQLines contain an integerM.

Output

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

Constraints
  • 1T3
  • 1N100000
  • 1Q30
  • 1M100
  • -10 ^ 9Number on card10 ^ 9
Example
Input25 11 2 -1 4 595 21 2 3 4 5515Output482
Explanation

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

Test Case #2, Query #1
{}, {5}, {}, {2, 3}, {, 5}, {2, 3}, {1, 3, 3}, {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, 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 <4 MB.


Solution:

The meaning of the question is very tedious. In fact, it is a sentence to ask the subset elements of a set and the number of subsets of m (Modulo. It looks a bit like a backpack, but it's not all about it.

The complexity of m can be reduced to a very low level.

Train of Thought 1, O (n * m * q)

Dp [I] [j] indicates a set composed of the number of I, and its subset is the number of types of j.

Transfer is O (1): dp [I] [j] = dp [I-1] [j-num [I] + dp [I-1] [j]

Even in this case, it actually times out. N = 100000, m = 100, q = 30.

Train of Thought 2, O (m ^ 3 * q)

Given that n is relatively large and m is relatively small, n numbers must be repeated after m is modulo.

Cnt [r] indicates the number of m modulo remainder in n, 0 <= r <m, so n numbers are divided into m heap, each Heap has cnt [r. Note that cnt [r] is also very large.

For the r heap, its own combination, r, 2r, 3r... after Modulo m, there are still up to m, m <cnt [r].

Therefore, add [r] [j] indicates that only the r heap is considered and all types of j are combined. Add [r] [r * t % m] = Sum (C (cnt [r], t)

After adding is obtained, the final result can be calculated through the transfer of m ^ 3.

Dp [0] [0] = 1;
FOR (I, 0, m)
FOR (j, 0, m) // rep j
FOR (r, 0, m) // combine r
Dp [I + 1] [j] = (dp [I + 1] [j] + dp [I] [(j-r + m) % m] * add [I] [r] % MOD) % MOD;

The complexity of computing the add operation is O (n). The calculation of the number of combinations must be optimized. Otherwise, the operation still times out.

The complexity of calculating the dp is m ^ 3,

Therefore, complexityO (m ^ 3 * q ).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.