2014 first I question in multiple schools | HDU 4869 turn the pokers (ferma's small theorem + Fast Power modulus)

Source: Internet
Author: User
Tags modulus mul

Question Link

Q: m cards, which can be turned over n times, Xi cards each time, and how many forms can be obtained at the end.

Idea: 0 is defined as the opposite, 1 is defined as the Front (both at the beginning), for each flop operation, we define two boundary LB, Rb, it indicates the minimum number of values in one and Rb indicates the maximum number of values in one. The parity of a card is the same as that of the two cards. Therefore, the parity of LB and Rb is the same. If lb and Rb are found, the numbers that are between the two numbers and are the same as those of the two odd couples can be obtained, then sum the number of combinations in this range (if lB = 3, rB = 7, then 3, 5, 7 can all be obtained, that is to say, the final result is to select 3 cards and then select 5 cards and 7 cards ). Therefore, you must first obtain the boundary according to the actual situation.

Because the data is quite large, division in the combination should be converted into multiplication. C (n, m) = n! /(M! * (N-m )!), From the Fermat's theorem: If P is a prime number, a ^ (p-1 = 1% P, then a ^ (P-2) = 1/A % P. Using this formula, get 1/(m! * (N-m )!) = (M! * (N-m )!) ^ (P-2) mod P, that is, C (n, m) = n! * (M! * (N-m )!) ^ (P-2) mod P, which can be changed to multiplication. And (n-m )!) ^ (P-2) mod p simplifies the computation with a fast power.

Here the code of this person is more detailed about the boundary explanation.

Official question:

The final result must appear consecutively. You only need to find the final interval.

Because if you perform two operations on the same card, the card status does not change. Therefore, the number of times the card is flipped must be reduced by an even number. If the sum of all numbers is an odd number, the final result must be an odd number. Likewise, even numbers are the same.

So we only need to recursively obtain the final interval, calculate sum (C (XI, M) (I =, 2 ...)), M is the total number of cards, Xi is the consecutive odd or even number in the interval, and 10 ^ 9 + 9 is the final answer.

 

1 # include <cstdio> 2 # include <cstring> 3 # include <iostream> 4 5 using namespace STD; 6 7 # define mod 1000000009 8 # define ll _ int64 9 10 ll f [100010]; 11 void MUL () 12 {13 f [0] = 1; 14 For (INT I = 1; I <100010; I ++) 15 f [I] = (F [I-1] * I) % MOD; 16} 17 18 ll multimod (ll a, LL B) 19 {20 ll res = 1; 21 while (B) 22 {23 if (B & 1) 24 {25 res * = A; 26 res % = MOD; 27} 28 B >>= 1; 29 A * = A; 30 A % = MOD; 3 1} 32 return res; 33} 34 int main () 35 {36 int n, m; 37 MUL (); 38 While (scanf ("% d ", & N, & M )! = EOF) 39 {40 int X; 41 scanf ("% d", & X); 42 int lB = x, rB = x; 43 for (INT I = 1; I <n; I ++) 44 {45 scanf ("% d", & X); 46 int L = LB-X; 47 int r = RB + X; 48 if (L <0) {49 If (RB-x <= 0) L = x-Rb; 50 else l = (Lb-x) % 2 + 2) % 2; // similar to an odd card, an odd card must end with an even number, and an odd card must be an odd number, there must be an intermediate state to get the final non-0: 151 // at this time, when the LB is in the LB <x <RB, when X cards are turned over, if the parity of x and LB, when the Rb is the same, it indicates that the flop has reached a certain State. That is to say, the remaining x sheets can be reversed. If the parity is different, it means either one more or one less, so it is 1.52} 53 If (r> m) {54 if (Lb + x <= m) r = m-(RB + x) % 2; 55 else r = m-(Lb + X-m); 56} 57 lB = L; 58 RB = r; 59} 60 ll ans = 0; 61 for (INT I = LB; I <= RB; I + = 2) 62 ans + = (F [m] % mod) * (multimod (f [I] * f [M-I]) % mod, mod-2) % MOD; 63 cout <ans % mod <Endl; 64} 65 return 0; 66}
View code

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.