Hdoj--4869--turn the Pokers "combination math + fast Power"
Test instructions: There is m card poker, start all face down, you can turn n times, every time you can turn XI Zhang, the remake of the rule is facing down on the back face down, and vice versa, ask how many times the direction of the card after the N-flop. We can interpret the face up as 1, and the opposite side up to 0, then it can be understood that there are several different combinations of 01 strings.
Problem-solving ideas: We can know that each card assumes that the starting state is 0, if the number of odd times, the last case of the card is 1, if the number of times, the last case of the card is 0. According to the number of N-flop to find the lower and upper limit of the number of 1, and then in this range with combinatorial mathematics to
Let's first look at the upper and lower limits of the number of 1:
MINM: Lower limit MAXM: Upper limit P: Current lower limit Q: Current upper limit
1. Ask for the lower limit of the number of 1:
The current lower limit is equal to the number of flop now, this better understanding, full turn to 0, then the remaining 1 is minm-x;
The current lower limit is less than the number of the flop, the upper limit is equal to the number of the flop, that is, the number of the flop is just between the upper and lower limits, so at least you can reduce the number of faces up to zero, but not necessarily reduced to 0, because there may be the current face up the card when the odd number, and the Why should I compare parity with MINM? If the parity is the same, it can be reduced to 0,
Otherwise, it should be 1.
When the number of flop is larger than the upper limit, the direct minus cap is the lower limit.
2. Ask for the upper limit of the number of 1:
Upper Limit + The number of the flop does not reach the total number of cards, the upper limit + the number of cards is the new upper limit, all turn 0, so that 1 the most;
The upper limit + the number of the flop is greater than the total number of cards, and the lower limit + the number of the flop is less than the total number of cards, the former can be said to be the flop overflow, has been all 1 re-turn the words will only let some 1 into 0, the latter did not achieve all become 1 of the situation. They are a limit of a lower limit, which means that can handle the situation in between, then the best result is that all cards are face up, all is 1, need to determine whether parity is consistent, this time and M comparison.
Upper limit + number of flop, lower limit + number of flop all is greater than the total number of cards, the description will overflow, then use 2 * M-(x + MINM) to represent the upper limit, because (X+MINM) small, so the overflow of 1 becomes 0 of the number of cards less.
The next thing to do is to calculate the value of C (M, i):
Due to the large number, to 1000000009 to take the remainder, here to use the fast power to take the remainder:
The template is as follows:
- Long long powermod (int a, int b, int c)
- {
- int ans = 1;
- A = a% c;
- while (b>0) {
- if (b% 2 = = 1)
- Ans = (ans * a)% C;
- b = B/2; b>>=1;
- A = (A * a)% C;
- }
- return ans;
- }
for quick exponentiation, you can view the link : http://blog.csdn.net/acm_code/article/details/38270829
Use the array c to represent the value of the combinatorial math C (M, i), supposedly c[i] = c[i-1] * (m-i + 1)/I, then this number to mod modulo, but the existence of division modulo is not so simple decomposition, Fermat theorem is this: a^ (p-1) ≡1 (mod p), p is prime, A, p coprime, a^ (p-1) mod p constant equals 1.
Change it, divide both sides by a, become a^ (p-2) =a^ ( -1) (mod p), so to divide by a can be expressed as a multiply a^ (p-2), so with this way: c[i] = c[i-1] * (m-i+1)% mod * POWERMOD (i,mod-2 )% MOD;
Finally, when the combined mathematical values are added together, it is not difficult to find the parity between the upper and lower bounds.
Sum up the above thought, we can write the code:
1#include <stdio.h>2 #defineMOD 10000000093__int64 c[100100];4__int64 mode (__int64 A,intN)5 {6__int64 T =A;7__int64 ans =1;8 while(n)9 {Ten if(N &1) One { AAns = ans * t%MOD; - } -N >>=1; thet = t%MOD; - } - returnans; - } + intMain () - { + intminm,maxm,x; A intp,q; at intn,m; - while(SCANF ("%d%d", &n,&m)! =EOF) - { - //The upper and lower limit of 1 in the initial state is 0; -MINM = MAXM =0;//MAXM represents the upper limit, MINM represents the lower limit -p = q =0;//p, q record the current lower limit, and then update to MINM, MAXM in for(intI=0; i<n; i++) - { toscanf"%d", &x);//number of the first I-flop + //judging the lower limit - if(minm>=x)//the current lower limit is greater than or equal to the number of tiles now thep = minm-x;//full turn to 0, then the remaining 1 is minm-x * $ Else if(maxm>=x)//the current lower limit is less than the number of the flop, the upper limit is equal to the number of cardsPanax Notoginsengp = ((x&1) = = (minm&1))?0:1;//x is 0 with minm parity, otherwise 1 - the Else//when the number of cards is larger than the upper limit, the direct minus limit is the lower limit. +p = x-MAXM; A the //judging the upper limit + if(maxm+x<=m)//Upper limit + number of flop does not reach the total number of cards, the upper limit + number of cards is the new limit -Q = MAXM +x; $ $ Else if(minm+x<=m)//Upper limit + number of flop is greater than the total number of cards, and the lower limit + number of cards is equal to the total number of cards -Q = ((minm+x) &1) = = (m&1))? m:m-1;//x is 0 with minm parity, otherwise 1 - the Else//Upper Limit + flop count, lower limit + flop count are all greater than the total number of cards -Q =2* M-(x +minm);Wuyi theMINM =p; -MAXM =Q; Wu } -__int64 sum=0; Aboutc[0]=1; $ for(intI=1; i<=maxm; i++)//request C (m,i); - { - if(m-i<i) -C[i] = c[m-i]; A Else + { theC[i] = c[i-1]* (m-i+1)%mod*mode ((__int64) i,mod-2)%MOD; - } $ } the for(intI=MINM; i<=maxm; i+=2) the { thesum+=C[i]; thesum%=MOD; - } inprintf"%i64d\n", sum); the } the return 0; About}
HDU 4869 Turn The Pokers (2014-School Union first game I)