HDU 4869 Turn The Pokers (combination number + Fermat theorem)

Source: Internet
Author: User

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 the change. She puts these pokers, she decided to the Flip Poker n times, and each time she can flip Xi pokers. She wanted to know how many the results does she get. Can solve this problem?

Inputthe input consists of multiple test cases.Each test case is begins with a line containing the 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 INPUT3 43 2 33 33 2 3

Sample Output83 HintFor the second example:0 Express face down,1 Express face upinitial State 000The first result:000->111->001->110t He second result:000->111->100->011the third Result:000->111->010->101so, there is three kinds of Results (110,011,101) test instructions: for M-cards give n operations, each operation choose A[i] Card to flip, ask finally get a few different states idea: In the N-card choose K-card, it is easy to think of the combination number, but the key is how to calculate the combined number? We can find that in the case of fixed number of cards, a total of the sum of the operation, in fact, a lot of cards have been flipped several times, and each flip only 0 and 12 states, then the parity is out, that is, no matter how the flop, the final state no matter how many 1, these 1 of the total number of parity is fixed. so now we just need to find the largest number of 1 and the smallest number of 1, then we need to find at least I 1, and the largest J 1;i is the case that there are 1 1,j the case is 0 turn 0, and the middle of the situation, take even steps, half turn 0, half Turn 1, remain unchanged, So you can be sure that i,i+2,i+4,..., j-2,j can be turned over. Last Ans=∑c (m,k) (i<=k<=j&&k%2==i%2). and then the solution of the combined number within this interval can be But there is another problem, the data is large, division is an unwise choice, but the combinatorial number formula must have Division C (n,m) = n!/(m!* (n-m)!) but we know Fermat theorem a^ (p-1) =1%p so a^ (p-1)/a = 1/a%p get a^ (p-2) = 1/a%p did you find it? This turns an integer into a denominator! so he got sum+= ((f[m]%mod) * (Quickmod ((f[i]*f[m-i)) %mod,mod-2)%mod)
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6 using namespacestd;7 #defineN 1000068 #definell Long Long9 #defineMOD 1000000009Ten ll N,m; One ll A[n]; A ll Fac[n]; - ll Pow_mod (ll a,ll i) - { the      if(i==0) -         return 1%MOD; -ll T=pow_mod (a,i/2); -ll ans=t*t%MOD; +      if(i%2==1) -ans=ans*a%MOD; +       returnans; A  } at ll work (ll m,ll i) -  { -      return((fac[m]%mod) * (Pow_mod (Fac[i]*fac[m-i]%mod, mod-2) (%mod))%MOD; -  } -  - intMain () in { -fac[0]=1; to      for(LL i=1; i<n;i++) fac[i]=fac[i-1]*i%MOD; +      -      while(SCANF ("%i64d%i64d", &n,&m) = =2) the     { * ll X; $ll l=0, r=0;Panax Notoginseng ll P,q; -          for(LL i=0; i<n;i++) the         { +scanf"%i64d",&x); A             //find the lower limit the             if(l>=x) p=l-x; +             Else if(r>=x) p= (l&1) = = (x&1)?0:1); -             Elsep=x-R; $              $             //ask for the upper limit -             if(r+x<=m) q=r+x; -             Else if(l+x<=m) q= ((l+x) &1) = = (m&1)? m:m-1); the             Elseq=2*m-(L +x); -             WuyiL=p; theR=Q; -         } Wull ans=0; -          for(inti=l;i<=r;i+=2) About         { $ans=ans+Work (m,i); -ans%=MOD; -         } -printf"%i64d\n", ans); A     } +     return 0; the}
View Code

Enclose the code of the Great God:

1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <algorithm>5 using namespacestd;6 #defineMoD 10000000097 #defineLL __int648 #defineMAXN 100000+59 Ten LL F[MAXN]; One  A void Set() - { -     inti; thef[0] =1; -      for(i =1; i<maxn; i++) -F[i] = (f[i-1]*i)%MoD; - } +  - ll Quickmod (ll A,ll b) + { ALL ans =1; at      while(b) -     { -         if(b&1) -         { -Ans = (ans*a)%MoD; -b--; in         } -B/=2; toA = ((a%mod) * (a%mod))%MoD; +     } -     returnans; the } *  $ intMain ()Panax Notoginseng { -     intN,M,I,J,K,L,R,X,LL,RR; the     Set(); +      while(~SCANF ("%d%d",&n,&m)) A     { theL = R =0; +          for(i =0; i<n; i++) -         { $scanf"%d",&x); $             //Calculate the minimum number of 1, as much as possible to let 1->0 -             if(l>=x) ll = l-x;//when the minimum 1 number is greater than X, turn x 1 all upside down -             Else if(r>=x) ll = ((l%2) = = (x2))?0:1;//when l<x<=r, because no matter how to turn, its parity must be equal, so I see the parity and X is the same, then know that the minimum must become 0, otherwise become 1 the             Elsell = X-r;//when the X>r, then the 1 all into 0, but also x-r 0 into 1 -             //calculate the maximum number of 1, as much as possible to let 0->1Wuyi             if(r+x<=m) rr = r+x;//in the case of R+x<=m, all becomes 1 the             Else if(l+x<=m) rr = (((l+x)%2) = = (m%2)? m:m-1);//in the case of r+x>m but l+x<=m, it is also a judgment parity, homomorphism so must be in the middle of a can all become 1, otherwise at least one must be 0 -             ElseRR =2*m-(L+X);//in the case of l+x>m, is equal to I first put M 1 into 0, then I have to turn (l+x-m), so I finally get M (l+x-m) a 1 Wu  -L = Ll,r =RR; About         } $LL sum =0; -          for(i = l; i<=r; i+=2)//summation using the Fermat theorem and the method of fast power -sum+= ((f[m]%mod) * (Quickmod ((f[i]*f[m-i))%mod,mod-2) (%mod))%MoD; -printf"%i64d\n", sum%MoD); A     } +  the     return 0; -}
View Code

HDU 4869 Turn The Pokers (combination number + Fermat theorem)

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.