Turn the pokers





Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 316    Accepted Submission(s): 101









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)

题目大意

给定M张牌,能够翻转N次。每次能够翻转恰好Xi张牌。刚開始牌面所有朝下,问经过N次翻转之后可能产生的扑克序列数(如例子hint)。

解题思路

现场还是没出……想到dp的思路但复杂度高达N^2.

能够观察到,我们最后正面朝上的牌的数量奇偶总是一定的(如1,3。5),由于不同奇偶情况就须要至少多翻一次,但翻动的次数已经固定不能更改。

考虑一次翻转,最多X1张牌正面朝上了。最少也是X1张。

假设第二次仅仅翻转1张,再设1<X1<m 则最多会有X1+1张牌正面朝上,最少会有X1-1张牌正面朝上。

如今如果我们已经翻好k-1次

最多a张正面朝上,最少b张正面朝上

假设b>=Xk 则翻转后最少有b-Xk张正面朝上(其余的情况能够一次翻一张正面的,一张反面的。情况保持不变)。

否则。假设a>Xk。这时最少会有0张或1张正面朝上(依据a和Xk的奇偶性推断),

再否则,最少会有Ak-a张正面朝上(先把正面朝上的所有翻转,剩下的再翻便是最少张数)

关于最大值的讨论同理

最后关于组合数的计算,因为m固定不变,就能够依据组合数公式C(m,n)=C(m-1,n)*(n-m+1)/m 线性求解。因为1e9+9是大素数。除法操作可用逆元求解取代。

#include <cstdio>
#define LL long long
int n,m;
LL mod=1000000009;
LL a[100005];
void egcd(LL a,LL b,LL &x,LL &y)
{
if (b==0)
{
x=1;y=0;
return ;
}
egcd(b,a%b,x,y);
LL t=x;
x=y,y=t-a/b*y;
return;
}
LL cal(LL x,LL y)
{
LL cur=1,tmp=0,t=0;
while(t<=y)
{
if (t>=x&&t<=y)
if ((t-x)%2==0) tmp=(tmp+cur)%mod;
t++;
cur=cur*(m-t+1)%mod;
LL t1,t2;
egcd(t,mod,t1,t2);
t1=(t1+mod)%mod;
cur=cur*t1%mod;
}
return tmp;
}
int main()
{
while (~scanf("%d%d",&n,&m)){
LL upper,lower,plower,pupper;
for (int i=1;i<=n;i++)
scanf("%I64d",&a[i]);
upper=lower=pupper=plower=0;
for (int i=1;i<=n;i++)
{
if (plower>=a[i]) lower-=a[i];
else if (pupper>=a[i]) lower=0+((pupper+a[i])%2==1);
else lower=a[i]-pupper;
if (pupper+a[i]<=m) upper+=a[i];
else if (plower+a[i]<=m) upper=m-((plower+a[i])%2==1);
else {upper=m-(plower+a[i]-m);}
plower=lower;//plower代表上一步的最小值
pupper=upper;//pupper代表上一步的最大值
}
printf("%I64d\n",cal(lower,upper));
}
return 0;
}

[hdu 4869](14年多校I题)Turn the pokers 找规律+拓欧逆元的更多相关文章

  1. Just Random HDU - 4790 思维题(打表找规律)分段求解

    Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...

  2. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  3. hdu 6045: Is Derek lying? (2017 多校第二场 1001)【找规律】

    题目链接 可以暴力找一下规律 比如,假设N=7,两人有5题相同,2题不同,枚举X=0->15时,Y的"Not lying"的取值范围从而找出规律 #include<bi ...

  4. HDU 5033 Building(北京网络赛B题) 单调栈 找规律

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

  5. hdu 6050: Funny Function (2017 多校第二场 1006) 【找规律】

    题目链接 暴力打个表找下规律就好了,比赛时看出规律来了倒是,然而看这道题看得太晚了,而且高中的那些数列相关的技巧生疏了好多,然后推公式就比较慢..其实还是自身菜啊.. 公式是 #include< ...

  6. JAVA常见算法题(三十二)---找规律

    题目一: 4,5,15,45,135,405,__ 题目二: 524,244,954,674,394,15,725, __ 题目三: 7,8,6,9,10,7,4,4,5,__ 求横线位置的整数. * ...

  7. HDU 5793 A Boring Question (找规律 : 快速幂+逆元)

    A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...

  8. hdu 3032 Nim or not Nim? (sg函数打表找规律)

    题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围  1 ≤ N ...

  9. hdu 5978 To begin or not to begin(概率,找规律)

    To begin or not to begin Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

随机推荐

  1. 查看 docker 容器使用的资源

    在容器的使用过程中,如果能及时的掌握容器使用的系统资源,无论对开发还是运维工作都是非常有益的.幸运的是 docker 自己就提供了这样的命令:docker stats. 默认输出 docker sta ...

  2. 小白必看Python视频基础教程

    Python的排名从去年开始就借助人工智能持续上升,现在它已经成为了第一名.Python的火热,也带动了工程师们的就业热.可能你也想通过学习加入这个炙手可热的行业,可以看看Python视频基础教程,小 ...

  3. 利用C#转换图片格式及转换为ico

    注意:转换为ICO后效果不好. 源代码: using System;using System.Collections.Generic;using System.Text;using System.Dr ...

  4. 很考验人的java内存加载面试题

    源代码如下,求结果 public class MemoryAnalyse { public static int k = 0; public static MemoryAnalyse t1 = new ...

  5. [转载] Java中动态加载jar文件和class文件

    转载自http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...

  6. Qemu 简述

    Qemu 架构 Qemu 是纯软件实现的虚拟化模拟器,几乎可以模拟任何硬件设备,我们最熟悉的就是能够模拟一台能够独立运行操作系统的虚拟机,虚拟机认为自己和硬件打交道,但其实是和 Qemu 模拟出来的硬 ...

  7. pt-log-player

    简介 pt-log-player是MySQL日志回放工具,在pt2.4中被去除,由percona-playback取代. 在2.1中还是保留,如果想使用的话,需要下载2.1版本的. 使用方法 pt-l ...

  8. MySQL长短密码

    MySQL长短密码 今天批量搭建MySQL环境的时候,遇到长短密码问题,故就此问题总结一下长短密码. 介绍 1.长密码例子: mysql> show grants for 'test'@'loc ...

  9. 【转】MYSQL 使用SQLyog导入遇到问题解决

    原文地址:http://blog.163.com/o5655@126/blog/static/1667428342010910112510738/  昨天公司想要将一个数据库的数据导出再导入到另外一个 ...

  10. java SE 基础概念梳理(一)

     (一)First 摘要:Java概述.Java开发环境搭建.程序开发流程 Java概述  Java的应用 开发QQ.迅雷程序(桌面应用软件)开发淘宝.京东(互联网应用软件) Java的擅长 互联网: ...