[Hdu 4869]

Source: Internet
Author: User

[Hdu 4869]
Turn the pokers


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 316 Accepted Submission (s): 101




Problem Description
During summer vacation, Alice stay at home for a long time, with nothing to do. she went out and bought m pokers, tending to play poker. but she hated the traditional gameplay. she wants to change. she puts these pokers face down, she decided to flip poker n times, and each time she can flip Xi pokers. she wanted to know how to handle the results does she get. can you help her solve this problem?
 


Input
The input consists of multiple test cases.
Each test case begins with a line containing two non-negative integers n and m (0 <n, m <= 100000 ).
The next line contains n integers Xi (0 <= Xi <= m ).
 


Output
Output the required answer modulo 1000000009 for each test case, one per line.
 


Sample Input


3 4
3 2 3
3 3
3 2 3
 


Sample Output
8
3


Hint
For the second example:
0 express face down, 1 express face up
Initial state 000
The first result: 000-> 111-> 001-> 110
The second result: 000-> 111-> 100-> 011
The third result: 000-> 111-> 010-> 101

So, there are three kinds of results (110,011,101)


Theme

Given M cards, you can flip them N times. Each time you flip the card, you can flip exactly the card Xi. At the beginning, all the cards face down, the number of poker sequences (such as the hint in the example) that may be generated after N flipped S ).

Solutions

The scene is still unavailable ...... The thought of dp is as complex as N ^ 2.

We can see that the number of cards with the last positive orientation is always equal to or equal to a certain value (such as, 5), because different parity situations require at least one flip, however, the number of flip operations cannot be changed.


Consider one flip. Up to X1 cards are facing up, and at least X1 cards are.

If you flip only one card for the second time, and then set 1 <X1 <m, there will be a maximum of X1 + 1 card front up, at least there will be X1-1 card front up.


Now let's assume we have turned over the K-1.

Up to a front-Up and at least B front-up

If B> = Xk, at least B-Xk faces face up after the flip (in other cases, you can flip one front and one back at a time, and the situation remains unchanged ).

Otherwise, if a> Xk, there will be at least 0 or 1 front-up (based on the parity of a and Xk ),

Otherwise, there will be at least Ak-a front-up (first flip all the front-up, and the remaining flip is at least)


The same is true for the discussion about the maximum value.


Finally, for the calculation of the number of combinations, since m is fixed and unchanged, we can use the formula C (m, n) = C (S-1, n) * (n-m + 1) of the number of combinations) /m linear solution. Because 1e9 + 9 is a large prime number, Division operations can be replaced by Reverse element solutions.

<Span style = "font-size: 14px;" >#include <cstdio> # define LL long longint n, m; LL mod = 1000000009; LL a [100005]; void egcd (LL a, LL B, LL & x, LL & y) {if (B = 0) {x = 1; y = 0; return ;} egcd (B, a % B, x, y); LL t = x; x = y, y = t-a/B * y; return ;} LL cal (LL x, LL y) {LL cur = 1, tmp = 0, t = 0; while (t <= y) {if (t> = x & t <= y) if (t-x) % 2 = 0) tmp = (tmp + cur) % mod; t ++; cur = cur * (m-t + 1) % mod; LL t1, t2; egcd (t, mod, t1, t2 ); t1 = (t1 + mod) % mod; cur = c Ur * t1 % mod;} return tmp;} int main () {while (~ Scanf ("% d", & n, & m) {LL upper, lower, plower, pupper; for (int I = 1; I <= n; I ++) scanf ("% I64d", & a [I]); upper = lower = pupper = plower = 0; for (int I = 1; I <= n; I ++) {if (plower> = a [I]) lower-= a [I]; else if (pupper> = a [I]) lower = 0 + (pupper + a [I]) % 2 = 1); else lower = a [I]-pupper; if (pupper + a [I] <= m) upper + = a [I]; else if (plower + a [I] <= m) upper = m-(plower + a [I]) % 2 = 1); else {upper = m-(plower + a [I]-m );} plower = lower; // plower indicates the minimum pupper = upper in the previous step; // pupper indicates the maximum value in the previous step} printf ("% I64d \ n", cal (lower, upper);} return 0 ;}</span>







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.