HDOJ 4869 Turn the pokers


最后的结果中正面向上的奇偶性是一定的,计算出正面向上的范围low,up

结果即为 C(m,low)+ C(m,low+2) +.... + C(m,up) ,用逆元取模

Turn the pokers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 828    Accepted Submission(s): 302


Problem Description
During 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 many the results does she get. Can you help her solve this problem?
 

Input
The input consists of multiple test cases. 
Each test case begins with a line containing two non-negative integers n and m(0<n,m<=100000). 
The next line contains n integers Xi(0<=Xi<=m).
 

Output
Output the required answer modulo 1000000009 for each test case, one per line.
 

Sample Input
  
  
3 4 3 2 3 3 3 3 2 3
 

Sample Output
  
  
8 3
Hint
For the second example: 0 express face down,1 express face up Initial state 000 The first result:000->111->001->110 The second result:000->111->100->011 The third result:000->111->010->101 So, there are three kinds of results(110,011,101)
 

Author
FZU
 

Source
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

const int maxn=110000;
const LL mod=1000000009LL;

LL a[maxn],n,m;

void ex_gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
	if(!b)
	{
		d=a; x=1; y=0;
	}
	else
	{
		ex_gcd(b,a%b,d,y,x);
		y-=a/b*x;
	}	
}

LL lower,upper;

void cal(LL lower,LL upper)
{
    LL cur=1LL,t=0,ret=0;
    while(t<=upper)    
    {
        if(t>=lower&&t<=upper)
            if((t-lower)%2==0)
                ret=(ret+cur)%mod;    
        t++;
        cur=(cur*(m-t+1))%mod;
        LL d,x,y;
        ex_gcd(t,mod,d,x,y);
        x=(x+mod)%mod;
        cur=(cur*x)%mod;
    }
    cout<<ret<<endl;
}

int main()
{
while(cin>>n>>m)
{
    lower=upper=0LL;
    for(int i=0;i<n;i++)
        cin>>a[i];
    lower=upper=a[0];
    for(int i=1;i<n;i++)
    {
        LL last_low=lower,last_up=upper;

        ///get low bound
        if(a[i]<=last_low)
            lower-=a[i];
        else if(a[i]<=last_up)
            lower=0+((a[i]+last_up)%2==1);
        else
            lower=a[i]-last_up;

        ///get upper bound
        if(a[i]+last_up<=m)    
            upper=a[i]+last_up;
        else if(a[i]+last_low<=m)
            upper=m-((a[i]+last_low)%2==1);
        else
            upper=m-(a[i]+last_low-m);
    }
    cal(lower,upper);
}
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值