HDU 4869 turn the pokers multi-school training first game 1009

Source: Internet
Author: User

Turn the pokers Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 282 accepted submission (s): 89


Problem descriptionduring 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 th Is problem?
Inputthe 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 ).
Outputoutput the required answer modulo 1000000009 for each test case, one per line.
Sample Input
3 43 2 33 33 2 3
 
Sample output
83HintFor the second example:0 express face down,1 express face upInitial state 000The first result:000->111->001->110The second result:000->111->100->011The third result:000->111->010->101So, there are three kinds of results(110,011,101) 
 
Authorfzu
Source2014 multi-university training contest 1

Question

These days are the most annoying. The question "facing posture is not enough... The algorithm I used during the competition was: "O (nlgn) sorting-O (n) computing the answer range-O (n) summation of the answer ". But it's tle... Later, I thought about it carefully. In fact, there is no need to sort it, and I can calculate the answer range by O (n. Then I re-wrote the part and struggled with the parity of bit operations for half a day.

I will talk about how my algorithms come from.

Abstraction

This model is very clever, because it is a card flip, then each card has two States (front '1' and back '0 ').

Can we abstract this actual example?

Of course!

First, introduce the Symbol M (n, k): indicates that among N cards, there are only all possible sets of K faces.

Next, I introduce a binary operation based on elements in M (n, k). The flip operation is represented by multiplication.

Why is binary calculation? We slowly consider the flip status. "Flip three from four cards ." Can this sentence be understood as "_ m (4, 0) * _ m (4, 3? I use a prefix underline to represent an element of this set. That is to say, this expression starts status transfer from the front. We do not consider the result for the moment. The state transfer method is to flip three sheets. In other words, a single Multiplication operation is equivalent to a cumulative flip. The first flip is 0, so all the answers are m (4, 0); the second flip is 3, so all the answers are m (4, 3 ). The sum of two flip operations is our answer.

Again, a single Multiplication operation is equivalent to a cumulative flip. Of course, multiple multiplication operations (for example, three first, two second, and finally three more) you can also view the accumulated flip for multiple times. That is, for a specific card, if it is flipped for an even number of times, it is equivalent to not flipped; if it is flipped for an odd number of times, it is equivalent to flipped once.

Then what is the scope of this operation? That's right. It's the whole of all the elements of the just-defined Symbol M (n, k), that is, G = {x | x ε M (n, k), k = 0, 1, 2, 3 ,..., N .}.

Next, we will focus on the nature of this operation: (for convenience, we will call this operation multiplication later)

  1. First, this operation satisfies the combination law. That is, for any element a, B, c, a * B * C = A * (B * C) in G ). This is not mentioned here. independent proof.
  2. Second, this operation has units E = m (n, 0 ). That is, for element a in any G, A * E = E * A =. This can be easily discovered.
  3. Furthermore, this operation has a reverse A-1 =. That is, for any G Element A, A * A-1 = A-1 * A = E. Because one flip task only needs to be executed once again.
  4. Finally, this operation has an exchange law. That is, for any element A, B, C, A * B = B * A in G. Because the order of Flip is not fixed, you can flip a sheet first, then flip B sheets; you can also flip B sheets first, then flip a sheet.

Therefore, the operation * for G and the operation built on G is an Abel group (exchange group ).

However, this algebra system is obviously not good enough. We should continue to expand him.

In other words, we just studied the nature of the two elements to perform this operation. Now we are going to study the nature of the two sets to perform this operation. Of course, first we start with the results and try to find the rules.

For example, in this calculation: m (n, X1) * m (n, X2), to facilitate the analysis results, we suppose x1> = x2. What kind of results will it produce?

Return to the previous definition. m (n, X1) indicates there are Y1: = x1 frontend and Y2: = (n-X1) backend. The next step is to flip the two cards. Let's assume that it re-flip the card I flipped over the last time, and flip the card J never flipped over. In this way, there is a possibility of C (Y1, I) + C (Y2, J), and of course it must satisfy the formula of the number of combinations. What are the results generated here? Assume that there are Z at the end, and Y1 at the beginning. Reduce I at first and increase J at the end. Because I + J = x2, x1> = x2, there are x1-X2 <= z <= min (n, (n-X1) + (n-X2 )). Moreover, if you write a script on the draft, you will find that the original z = Y1-I + J = x1-I + (x2-I) = X1 + x2-2 * I. That is, z is an arithmetic difference sequence with a tolerance of 2.

For ease of understanding, I introduced the addition symbol + to represent the parallel operation of the Set.

For example:

M (4, 3) * m (4, 2) = M (4, 1) + M (4, 3)

M (8, 5) * m (8, 3) = M (8, 2) + M (8, 4) + M (8, 6) + M (8, 3, 8)

M (8, 4) * m (8, 6) = M (8, 3) + M (8, 5) + M (8, 7)

I don't know if there is a rule, that is, the parity of the result sequence. If X1 and X2 are both an even number, the result is an odd number sequence. If the result is an odd number, the result is an even number sequence. If the result is an odd number, the result is an odd number sequence. There is another rule. If there are multiple multiplication operations, isn't it necessary to perform another Multiplication operation on each generated result? That's right. Strictly speaking, the introduction of addition also introduces the concept of allocation rate, which is equivalent to the establishment of a ring. However, it is not very useful, but it is easy to understand.

Return to the problem of multiple multiplication operations. If every Multiplication operation is performed, the answer will be very slow, because here it is equivalent to expanding the data of N to (n/2) A group of new data is exponential. For example, if I calculate this set of multiplication:

M (8, 4) * m (8, 6) * m (8, 3)

When the running result of the first multiplication is:

M (8, 4) * m (8, 6) = M (8, 3) + M (8, 5) + M (8, 7)

Next, go to multiply one by one:

M (8, 3) * m (8, 3) = M (8, 0) + M (8, 2) + M (8, 4) + M (8, 6)

M (8, 5) * m (8, 3) = M (8, 2) + M (8, 4) + M (8, 6) + M (8, 3, 8)

M (8, 7) * m (8, 3) = M (8, 4) + M (8, 6)

We can see that the actual answer is m (8, 4) * m (8, 6) * m (8, 3) = M (8, 0) + M (8, 2) + M (8, 4) + M (8, 6) + M (8, 8)

In other words, isn't it convenient if we record the upper and lower bounds? In this way, it will not be expanded layer by layer.

Therefore, the problem is abstracted to calculate the intervals of each layer and consider parity.

Calculate the interval and record parity

Each time I update the last answer interval. In fact, it is more accurate to check whether the range needs to be enlarged. In particular, it is used to determine whether the X (equivalent to k in M (n, k) in the preceding range) is not in this range, and the correct value assignment is enough, that is, low = 0, high = n. The rest determine the distance between the boundary of the current interval. One value is a small value and the other value is a vertex.

Of course, you can't forget to deal with parity. Parity and exclusive or operations are similar, so I use different or operations.

Finally, because it is a sequence with a tolerance of 2, we only record the range and parity. Therefore, the answer should be judged based on parity.

The overall time complexity is O (n) {calculation interval}-O (n) {calculation answer }.

Calculation of the combination number

The formula for calculating the number of combinations is simple. C (n, m) = n! /(M! * (N-m )!)

In the modulo operation, division A/B is equivalent to a * B ^-1, that is, multiplication of its inverse. So we can calculate the factorial of the inverse element in the pre-processing.

The definition of reverse element is that B that satisfies a * B % P = 1 is called the reverse element of A. Sometimes it is recorded as B = inv (). The calculation method is simple. Do you still remember to extend Euclidean? Ax + by = gcd (A, B ). Because the adjacent two numbers must be mutually unique, it can be written as AX + by = 1. This indefinite equation can be transformed from a linear homogeneous equation with a single element. Therefore, we can quickly obtain the inverse element by extending Euclidean. Of course, it can also be calculated directly through recursive solutions.

After the inverse element is calculated, it is the query result. This is to note that the modulo is required for each multiplication.

Sample Code
/*************************************** **************************************** Copyright Notice * copyright (c) 2014 All Rights Reserved * ---- stay hungry Stay Foolish ---- ** @ Author: Shen * @ name: HDU 4869 turn the pokers * @ file: G: \ My source code \ [ACM] competition \ 0722-mutc [1] \ I. CPP * @ Date: 2014/07/22 13:52 * @ algorithm: group theory, number theory, combination *************************************** *********************************** * ***/# Include <cstdio> # include <iostream> # include <algorithm> using namespace STD; Template <class T> inline bool updatemin (T & A, t B) {return A> B? A = B, 1: 0;} template <class T> inline bool updatemax (T & A, t B) {return a <B? A = B, 1: 0;} typedef long int64; typedef pair <int, int> range; // The Answer range // first-> lowerbound, second-> upperboundconst int maxm = 100005; const int64 mod = 1000000009; int64 inv [maxm]; // reverse element, a * inv (a) % P = 1int64 FAC [maxm]; // factorial, 1*2*3 *... int64 RFC [maxm]; // inverse factorial, inv (1) * inv (2) * inv (3) * ...int n, m; int X [maxm]; void Init () {inv [0] = inv [1] = 1; FAC [0] = FAC [1] = 1; RFC [0] = RFC [1] = 1; for (INT I = 2; I <maxm; I ++) {inv [I] = (mod-mod/I) * inv [mod % I]) % MOD; FAC [I] = (FAC [I-1] * I) % MOD; RFC [I] = (RFC [I-1] * inv [I]) % mod ;}} inline int64 C (int64 N, int64 K) {return (FAC [N] * RFC [k] % mod) * RFC [n-k] % MOD;} inline bool CMP (int A, int B) {return A> B;} range Update (INT X, range & cur, bool & isodd) {int low = 0, high = 0; int curl = cur. first, curh = cur. second; // Update isodd) isodd ^ = (X % 2 = 1); // update lower bound if (curl <= x & x <= curh) Low = 0; else low = min (ABS (curl-x), ABS (curh-x); // update upper bound x = N-X; if (curl <= x & x <= curh) High = N; else high = max (n-ABS (curl-x ), n-ABS (curh-x); Return make_pair (low, high);} void solve () {for (INT I = 0; I <m; I ++) scanf ("% d", & X [I]); range res = make_pair (0, 1); bool ISO Dd = 0; For (INT I = 0; I <m; I ++) RES = Update (X [I], res, isodd); int64 ans = 0; for (INT I = res. first; I <= res. second; I ++) if (I % 2 = 1) = isodd) ans = (ANS + C (n, I) % MOD; cout <ans <Endl;} int main () {Init (); While (~ Scanf ("% d", & M, & N) solve (); Return 0 ;}



HDU 4869 turn the pokers multi-school training first game 1009

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.