HDU 4869 turn the pokers (more than 2014 schools join the first I)

Source: Internet
Author: User

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


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?

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4869


Train of Thought: Unfortunately, I did not think about it during the competition. I thought about it as soon as it came out.

First, consider two flipped s, one flipped S (A) and one flipped S (B). If A> B is set, consider the number of numbers on the front after the flipped S (assuming the numbers are on the back at the beginning, the following uses 0 to indicate the back is up, and 1 to indicate the front is up ). After turning, the maximum number of 1 values may be a + B, and the minimum value is a-B. Further observation shows that after two flip operations, the value of 1 can be a-B at a minimum, A + B at a interval of 2, that is, a-B, a-B + 2, a-B + 4 ...... A + B-2, A + B. Now, if we add a number C, the number of 1 will be an interval, so we only need to maintain this interval. Note that there will be such a situation, such as a + B over N or a-B less than 0. In this case, we need to discuss and calculate a new interval. This is just a matter of detail. After calculating the number range [L, R] of 1, the final answer is C (n, L) + C (n, l + 2) + C (n, L + 4) + ...... C (n, R), and finally take the modulo 1e9 + 7. (C (n, m) is the number of M combinations in n.) Because N is relatively large, therefore, you cannot directly follow the formula C [N] [m] = C [n-1] [m] + C [n-1] m-1] Because C (n, m) = n! /(M! * (N-m )!), Then, n is pre-processed! And n! For the inverse element of 1e9 + 7, you can obtain the answer based on the formula.


#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#define ll long long#define maxn 100010#define mod 1000000009using namespace std;ll pow(ll x,ll y){    if(y==0)    return 1;    ll tmp=pow(x,y/2);    tmp=(tmp*tmp)%mod;    if(y&1)    tmp=tmp*x%mod;    return tmp;}ll c[maxn],b[maxn];void init(){    c[0]=1;    for(int i=1;i<=100000;i++)    c[i]=(c[i-1]*i)%mod;    for(int i=0;i<=100000;i++)    b[i]=pow(c[i],mod-2);}ll getC(int n,int m){    if(m==0||m==n)    return 1;    ll tmp=c[n]*b[m]%mod*b[n-m]%mod;    return tmp;}int main(){    freopen("dd.txt","r",stdin);    init();    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        int x;        int l=0,r=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            int rr=r;            if(r+x<=m)            r+=x;            else            {                if(l+x<=m)                {                    if((m-l-x)%2)                    r=m-1;                    else                    r=m;                }                else                {                    r=m-(l+x-m);                }            }            if(l-x>=0)            l-=x;            else            {                if(rr>=x)                {                    if((rr-x)%2)                    l=1;                    else                    l=0;                }                else                l=x-rr;            }        }        ll ans=0;        for(int i=l;i<=r;i+=2)        {            ans=(ans+getC(m,i))%mod;        }        printf("%I64d\n",ans);    }    return 0;}


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.