Original question: Turn the pokers
Idea: assume that the front is 0 and the back is 1. Like this 000000 ........ Considering that if we can achieve the final opposite side of the number of M, a total of N cards, the N groups of m as the opposite side of the other are all positive conditions can be achieved. So it is converted to consider which of the first possible number of sets will eventually appear.
Because the possible number set is continuous (which may differ by 2 within the maximum and minimum values), the upper and lower limits after each flip can be introduced according to the upper and lower limits obtained last time.
Finally, the recursive formula of the number of combinations and the deduction of the Fermat theorem are used to calculate the permutation and combination \ (a ^ {P-2} \ equiv a ^ {-1} \ bmod p \), calculate the reverse element using the quick power method.
In fact, TLE has been used n times ...... Bit operations simplify... The input must be scanf fast enough.
1 # include <iostream>
2 # include <fstream>
3 # include <cstring>
4 # include <cstdio>
5 # include <algorithm>
6 # include <cmath>
7 // # define local
8 # define fin CIN
9 # define fout cout
10 # define ll long int
11 # define maxn 100000 + 5
12 using namespace STD;
13 LL Mm = 1000000009;
14 ll c [maxn];
15 ll quickmod (ll a, int B)
16 {
17 ll ans = 1, base =;
18
19 while (B! = 0)
20 {
21 if (B & 1)
22 {
23 ans = ans * base % mm;
24}
25 B> = 1;
26 base = base * base % mm;
27}
28
29 return ans;
30}
31 int main ()
32 {
33 # ifdef local
34 ofstream fout ("1.out ");
35 ifstream fin ("1.in ");
36 # endif
37
38 int I, J, K;
39 int n, m, X;
40
41 memset (C, 0, sizeof (c ));
42
43 while (Fin> N> m)
44 {
45
46 int left, right, A1, A2;
47 left = 0; Right = 0;
48
49 for (I = 0; I <n; I ++)
50 {
51 scanf ("% d", & X );
52
53
54 if (x <= left) {a1 = left-X ;}
55 else if (x <= right)
56 {a1 = (left & 1) = (X & 1 ))? ;
57}
58 else {
59 a1 = x-right;
60}
61
62 if (x <= m-Right) {a2 = right + X ;}
63 else if (x <= m-left)
64 {
65 a2 = (m-left) & 1) = (X & 1 )? M-1 );
66}
67 else {
68 a2 = 2 * m-(x + Left );
69}
70
71 left = A1; Right = a2;
72
73}
74
75
76 c [0] = 1; C [m] = 1;
77
78 for (I = 1; I <= m/2 + 1; I ++)
79 {C [I] = C [I-1] * (M-I + 1) % mm * quickmod (I, MM-2) % mm;
80
81 C [M-I] = C [I];
82}
83
84
85 ll sum = 0;
86 for (I = left; I <= right; I + = 2)
87 {sum + = C [I];
88 sum % = mm;
89}
90
91 fout <sum <Endl;
92}
93
94
95 # ifdef local
96 fin. Close ();
97 fout. Close ();
98 # endif
99
100 return 0 ;}