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个操作,每次操作选择a[i]张牌进行翻转,问最终得到几个不同的状态
思路:在n张牌选k张,很容易想到组合数,但是关键是怎么进行组合数计算呢?我们可以发现,在牌数固定的情况下,总共进行了sum次操作的话,其实有很多牌是经过了多次翻转,而每次翻转只有0和1两种状态,那么,奇偶性就出来了,也就是说,无论怎么进行翻牌,最终态无论有几个1,这些1的总数的奇偶性是固定的。
那么我们现在只需要找到最大的1的个数和最小的1的个数,之后我们需要找到最少有i个1,以及最大有j个1;i的情况就是有1就翻1,j的情况就是有0就翻0,而中间的情况时,取偶数步数,一半翻0,一半翻1,保持不变,所以可以确定i,i+2,i+4,...,j-2,j都能被翻到。最后ans=∑C(m,k)(i<=k<=j&&k%2==i%2)。
然后再这个区间内进行组合数的求解即可
但是又有一个问题出来了,数据很大,进行除法是一个不明智的选择,但是组合数公式必定有除法
C(n,m) = n!/(m!*(n-m)!)
但是我们知道费马小定理a^(p-1)=1%p
那么a^(p-1)/a = 1/a%p 得到 a^(p-2) = 1/a%p
发现了吧?这样就把一个整数变成了一个分母!
于是便得到sum+=((f[m]%mod)*(quickmod((f[i]*f[m-i])%mod,mod-2)%mod))%mod
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<stdlib.h>
  6. using namespace std;
  7. #define N 100006
  8. #define ll long long
  9. #define MOD 1000000009
  10. ll n,m;
  11. ll a[N];
  12. ll fac[N];
  13. ll pow_mod(ll a,ll i)
  14. {
  15. if(i==)
  16. return %MOD;
  17. ll t=pow_mod(a,i/);
  18. ll ans=t*t%MOD;
  19. if(i%==)
  20. ans=ans*a%MOD;
  21. return ans;
  22. }
  23. ll work(ll m,ll i)
  24. {
  25. return ( (fac[m]%MOD)* ( pow_mod(fac[i]*fac[m-i]%MOD ,MOD-)%MOD))%MOD;
  26. }
  27.  
  28. int main()
  29. {
  30. fac[]=;
  31. for(ll i=;i<N;i++) fac[i]=fac[i-]*i%MOD;
  32.  
  33. while(scanf("%I64d%I64d",&n,&m)==)
  34. {
  35. ll x;
  36. ll l=,r=;
  37. ll p,q;
  38. for(ll i=;i<n;i++)
  39. {
  40. scanf("%I64d",&x);
  41. //求下限
  42. if(l>=x) p=l-x;
  43. else if(r>=x) p=((l&)==(x&)?:);
  44. else p=x-r;
  45.  
  46. //求上限
  47. if(r+x<=m) q=r+x;
  48. else if(l+x<=m) q=(((l+x)&)==(m&)?m:m-);
  49. else q=*m-(l+x);
  50.  
  51. l=p;
  52. r=q;
  53. }
  54. ll ans=;
  55. for(int i=l;i<=r;i+=)
  56. {
  57. ans=ans+work(m,i);
  58. ans%=MOD;
  59. }
  60. printf("%I64d\n",ans);
  61. }
  62. return ;
  63. }

附上大神的代码:

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. using namespace std;
  6. #define mod 1000000009
  7. #define LL __int64
  8. #define maxn 100000+5
  9.  
  10. LL f[maxn];
  11.  
  12. void set()
  13. {
  14. int i;
  15. f[] = ;
  16. for(i = ; i<maxn; i++)
  17. f[i] = (f[i-]*i)%mod;
  18. }
  19.  
  20. LL quickmod(LL a,LL b)
  21. {
  22. LL ans = ;
  23. while(b)
  24. {
  25. if(b&)
  26. {
  27. ans = (ans*a)%mod;
  28. b--;
  29. }
  30. b/=;
  31. a = ((a%mod)*(a%mod))%mod;
  32. }
  33. return ans;
  34. }
  35.  
  36. int main()
  37. {
  38. int n,m,i,j,k,l,r,x,ll,rr;
  39. set();
  40. while(~scanf("%d%d",&n,&m))
  41. {
  42. l = r = ;
  43. for(i = ; i<n; i++)
  44. {
  45. scanf("%d",&x);
  46. //计算最小的1的个数,尽可能多的让1->0
  47. if(l>=x) ll = l-x;//当最小的1个数大于x,把x个1全部翻转
  48. else if(r>=x) ll = ((l%)==(x%))?:;//当l<x<=r,由于无论怎么翻,其奇偶性必定相等,所以看l的奇偶性与x是否相同,相同那么知道最小必定变为0,否则变为1
  49. else ll = x-r;//当x>r,那么在把1全部变为0的同时,还有x-r个0变为1
  50. //计算最大的1的个数,尽可能多的让0->1
  51. if(r+x<=m) rr = r+x;//当r+x<=m的情况下,全部变为1
  52. else if(l+x<=m) rr = (((l+x)%) == (m%)?m:m-);//在r+x>m但是l+x<=m的情况下,也是判断奇偶,同态那么必定在中间有一种能全部变为1,否则至少有一张必定为0
  53. else rr = *m-(l+x);//在l+x>m的情况下,等于我首先把m个1变为了0,那么我还要翻(l+x-m)张,所以最终得到m-(l+x-m)个1
  54.  
  55. l = ll,r = rr;
  56. }
  57. LL sum = ;
  58. for(i = l; i<=r; i+=)//使用费马小定理和快速幂的方法求和
  59. sum+=((f[m]%mod)*(quickmod((f[i]*f[m-i])%mod,mod-)%mod))%mod;
  60. printf("%I64d\n",sum%mod);
  61. }
  62.  
  63. return ;
  64. }

hdu 4869 Turn the pokers(组合数+费马小定理)的更多相关文章

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

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

  2. HDU 5667 Sequence 矩阵快速幂+费马小定理

    题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...

  3. HDU——5667Sequence(矩阵快速幂+费马小定理应用)

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  4. HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

    Sum Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  5. 数学--数论--HDU 1098 Ignatius's puzzle (费马小定理+打表)

    Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so h ...

  6. UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)

    题目:传送门. 题意:t组数据,每组给定n,m,k.有n个格子,m种颜色,要求把每个格子涂上颜色且正好适用k种颜色且相邻的格子颜色不同,求一共有多少种方案,结果对1e9+7取余. 题解: 首先可以将m ...

  7. HDU 4869 Turn the pokers (2014 Multi-University Training Contest 1)

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

  8. HDU4869: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(推理)

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

随机推荐

  1. 300元差价选谁好 魅蓝note对比魅蓝手机

    http://mobile.pconline.com.cn/608/6089437.html [PConline 对比评测]999元的魅蓝note和699元的魅蓝手机先后被发布,代表着魅族中低端手机已 ...

  2. [转]iOS设备唯一标识探讨

    转自:http://www.jianshu.com/p/b83b0240bd0e iOS设备唯一标识探讨 为了统计和检测应用的使用数据,几乎每家公司都有获取唯一标识的业务需求,在iOS5以前获取唯一标 ...

  3. JAVA-FileInputStream之read方法

    今天一个友询问FileInputStrem方法的read()和read(byte b) 方法为什么都用-1来判断读文件结束的问题,在此和大家一起学习下. 关于FileInputStream 它用于读取 ...

  4. (转)检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(转)

    我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.N ...

  5. iOS 数据持久化

    一.plist文件存储 获得文件 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDom ...

  6. initWithFrame和initWithCoder区别

    当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame{ if (self  ...

  7. 完整的Ajax实例

    写在前面的话: 用了很久的Asp.Net Ajax,也看了段时间的jquery中ajax的应用,但到头来,居然想不起xmlHttpRequest的该如何使用了. 以前记的也不怎么清楚,这次就重新完整的 ...

  8. C#小写人民币转大写

    public string GetRMB(decimal moneyAmount) { string s = moneyAmount.ToString("#L#E#D#C#K#E#D#C#J ...

  9. mysql 5.7.9(GA) 安装

    mysql 5.7.9(GA) 终于发布了,感受一下. 一.下载 下载页面 http://dev.mysql.com/downloads/mysql/ 选择相应系统的版本下载. 本文OS为centos ...

  10. CSS实现三角形效果

    类似三角形的形状的元素在网页中可能会用到,我们可以用图片或者CSS元素达到我们想要的效果.这里讲一下是讲自己使用HTML+CSS实现三角形的方式. 为了能够熟悉的使用HTML+CSS构建三角形,我们首 ...