Turn the pokers

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

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)

 

果然又是一道神题目,用到的知识点真心多且有用。
 
快速幂+费马小定理+巧妙的思路
 
费马小定理:假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p)因此 a * a^(p-2)  ≡1(mod p),即 a的乘法逆元是a^(p-2)
因此题目需要求组合数:( n!/m!(n-m)! ) mod p =[ n! mod p ]*[ (m! mod p)^(p-2) mod p]*[ ((n-m)! mod p)^(p-2) mod p] mod p
 
多搞几个数据,可以发现,只需找到最少翻动牌个数与最多翻动牌个数即可,中间的状态是连续的,剩下就是排列的问题了。
因为每次翻一张牌,则正面增加1,反面减1,因此翻动的牌数是偶数变化的,又翻的牌可以随机,所以其最多与最少翻动牌数中间每隔一个都是允许出现的情况。
 
官方题解:

最终的结果一定是连续出现的,只需要求出最终的区间。

因为如果对同一张牌进行两次操作,牌的状态不改变。故牌的翻转次数一定是减少偶数次。如果所有数的和是奇数,那么最终结果也一定是奇数。同理,偶数也是一样的。

所以只要递推求出最后的区间,计算sum(C(xi,m)(i=0,1,2。。。)),m是总牌数,xi是在区间内连续的奇数或偶数,在模10^9+9就是最终的答案。

#include <cstdio>
#include <iostream>
#include <cmath>
#define Mod 1000000009
#define max(x,y) ((x)>(y)?x:y)
#define min(x,y) ((x)<(y)?x:y)
using namespace std;
long long J[];
int n,m,a[],l,r,nl,nr;
void Predo(){
J[]=;
for(int i=;i<=;i++)
J[i]=(J[i-]*i)%Mod;
}
long long Q(long long a,long long p){
int e[],k=;
while(p){
e[k++]=p%;
p=p/;
}
long long tmp=;
for(int i=k-;i>=;i--)
if(e[i]) tmp=((tmp*tmp)%Mod*a)%Mod;
else tmp=(tmp*tmp)%Mod;
return tmp;
}
long long C(int n,int m){
return ((J[n]*Q(J[m],Mod-))%Mod*Q(J[n-m],Mod-))%Mod;
}
int main()
{
Predo();
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
int l=r=a[];
for(int i=;i<n;i++){
nl=min(abs(l-a[i]),abs(r-a[i]));
if(l<=a[i]&&a[i]<=r){
if((a[i]-l)%==) nl=;
else nl=;
}
nr=max(l+a[i]<=m?l+a[i]:*m-l-a[i] , r+a[i]<=m?r+a[i]:*m-r-a[i]);
if(m-r<=a[i]&&a[i]<=m-l){
if((a[i]-(m-r))%==) nr=m;
else nr=m-;
}
l=nl;
r=nr;
}
long long ans=;
for(int i=l;i<=r;i=i+)
ans=(ans+C(m,i))%Mod;
printf("%lld\n",ans);
}
return ;
}

HDU 4869 Turn the pokers (2014 Multi-University Training Contest 1)的更多相关文章

  1. HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4869 Turn the pokers (2014 多校联合第一场 I)

    HDOJ--4869--Turn the pokers[组合数学+快速幂] 题意:有m张扑克,开始时全部正面朝下,你可以翻n次牌,每次可以翻xi张,翻拍规则就是正面朝下变背面朝下,反之亦然,问经过n次 ...

  3. HDU 4869 Turn the pokers(推理)

    HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确 ...

  4. hdu 4869 Turn the pokers (2014多校联合第一场 I)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 4869 Turn the pokers (思维)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 4869 Turn the pokers(思维+组合公式+高速幂)

    pid=4869" target="_blank">Turn the pokers 大意:给出n次操作,给出m个扑克.然后给出n个操作的个数a[i],每一个a[i] ...

  7. 2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)

    题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少 ...

  8. hdu 4869 Turn the pokers(组合数+费马小定理)

    Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...

  9. HDU 4869 Turn the pokers(思维+逆元)

    考试的时候没有做出来... 想到了答案一定是一段连续的区间,一直在纠结BFS判断最后的可行1数. 原来直接模拟一遍就可以算出来最后的端点... 剩下的就是组合数取模了,用逆元就行了... # incl ...

随机推荐

  1. “Microsoft Visual Studio遇到了问题,需要关闭”解决办法

    运行VS2008,打开项目,弹出错误界面 . 解决办法:将项目中的所有设计窗体关闭并保存,重新打开就OK~

  2. 64bit Ubuntu, Android AAPT, R.java

    Ubuntu 13.10 aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object f ...

  3. SAP如何使用关于序列号的表

  4. JAVA SSH 框架介绍

    SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...

  5. Android ADB 端口占用问题解决方案

    问题描述: The connection to adb is down, and a severe error has occured. You must restart adb and Eclips ...

  6. c# 取 list前100条数据

    [问] List<KeyWord> sortedList = (from a in keyWordList orderby a.Total descending select a).ToL ...

  7. MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能

    MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...

  8. UVA 10954 Add All 哈夫曼编码

    题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...

  9. web系统之session劫持解决

    session劫持是一种比较复杂的攻击方法.大部分互联网上的电脑多存在被攻击的危险.这是一种劫持tcp协议的方法,所以几乎所有的局域网,都存在被劫持 可能. 两台主机要想进行TCP通信,必须经过一个三 ...

  10. MySQL性能优化的最佳20+条经验(转)

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我 们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...