codechef:April Challenge 2014: Cards, bags and coins

來源:互聯網
上載者:User

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

Statement

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 Ttest 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
Input25 11 2 -1 4 595 21 2 3 4 5515Output482
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.


解決思路:

題意說的很繁瑣,其實就是一句話,問一個集合的子集元素和為m(模數)的子集個數。看似有點像背包的思路,但不盡然。

因為這道題,對m模數,因此其複雜度可以降到很低。

思路1,O(n*m*q)

dp[i][j]表示前i個數構成的集合,其子集和為j的種類數。

轉移是O(1): dp[i][j]=dp[i-1][j-num[i]]+dp[i-1][j]

而即使是這樣,居然逾時了。n=100000,m=100,q=30.

思路2,O(m^3*q)

考慮到n比較大,而m比較小,因此n個數對m模數之後肯定重複的數很多。

cnt[r]表示n個數中對m模數餘數為r的個數,0<=r<m, 因此將n個數分為m堆,每一堆有cnt[r]個。注意cnt[r]也會很大。

對於第r堆,其自身的組合,r,2r,3r...對m模數之後,依然最多也只有m個,m<cnt[r].

因此add[r][j]表示僅考慮第r堆,組合成j的所有種類數。add[r][r*t%m] =Sum( C(cnt[r],t)

求得add之後,通過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;

計算add的複雜度是O(n),其中組合數的計算,需要最佳化,否則依然逾時。

計算dp的複雜度是m^3,

因此複雜度O(m^3*q)。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.