HDU4869:Turn the pokers(费马小定理+快速幂)
来源:互联网 发布:怎么来淘宝店铺 编辑:程序博客网 时间:2024/05/29 08:30
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).
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 43 2 33 33 2 3
Sample Output
83HintFor the second example:0 express face down,1 express face upInitial state 000The first result:000->111->001->110The second result:000->111->100->011The third result:000->111->010->101So, there are three kinds of results(110,011,101)
题意:对于m张牌给出n个操作,每次操作选择a[i]张牌进行翻转,问最终得到几个不同的状态
思路:在n张牌选k张,很容易想到组合数,但是关键是怎么进行组合数计算呢?我们可以发现,在牌数固定的情况下,总共进行了sum次操作的话,其实有很多牌是经过了多次翻转,而每次翻转只有0和1两种状态,那么,奇偶性就出来了,也就是说,无论怎么进行翻牌,最终态无论有几个1,这些1的总数的奇偶性是固定的。
那么我们现在只需要找到最大的1的个数和最小的1的个数,然后再这个区间内进行组合数的求解即可
但是又有一个问题出来了,数据很大,进行除法是一个不明智的选择,但是组合数公式必定有除法
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
用快速幂去撸吧!
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define mod 1000000009#define LL __int64#define maxn 100000+5LL f[maxn];void set(){ int i; f[0] = 1; for(i = 1; i<maxn; i++) f[i] = (f[i-1]*i)%mod;}LL quickmod(LL a,LL b){ LL ans = 1; while(b) { if(b&1) { ans = (ans*a)%mod; b--; } b/=2; a = ((a%mod)*(a%mod))%mod; } return ans;}int main(){ int n,m,i,j,k,l,r,x,ll,rr; set(); while(~scanf("%d%d",&n,&m)) { l = r = 0; for(i = 0; i<n; i++) { scanf("%d",&x); //计算最小的1的个数,尽可能多的让1->0 if(l>=x) ll = l-x;//当最小的1个数大于x,把x个1全部翻转 else if(r>=x) ll = ((l%2)==(x%2))?0:1;//当l<x<=r,由于无论怎么翻,其奇偶性必定相等,所以看l的奇偶性与x是否相同,相同那么知道最小必定变为0,否则变为1 else ll = x-r;//当x>r,那么在把1全部变为0的同时,还有x-r个0变为1 //计算最大的1的个数,尽可能多的让0->1 if(r+x<=m) rr = r+x;//当r+x<=m的情况下,全部变为1 else if(l+x<=m) rr = (((l+x)%2) == (m%2)?m:m-1);//在r+x>m但是l+x<=m的情况下,也是判断奇偶,同态那么必定在中间有一种能全部变为1,否则至少有一张必定为0 else rr = 2*m-(l+x);//在l+x>m的情况下,等于我首先把m个1变为了0,那么我还要翻(l+x-m)张,所以最终得到m-(l+x-m)个1 l = ll,r = rr; } LL sum = 0; for(i = l; i<=r; i+=2)//使用费马小定理和快速幂的方法求和 sum+=((f[m]%mod)*(quickmod((f[i]*f[m-i])%mod,mod-2)%mod))%mod; printf("%I64d\n",sum%mod); } return 0;}
0 0
- HDU4869:Turn the pokers(费马小定理+快速幂)
- HDU4869:Turn the pokers(费马小定理+快速幂)
- <模板>Hdu4869 Turn the pokers 组合数求余 费马小定理
- HDU4869 Turn the pokers
- HDU4869:Turn the pokers[错误分析
- 贪心+组合数 hdu4869 Turn the pokers
- hdu4869 Turn the pokers 2014 Multi-University Training Contest 1
- HDU -- 4869 Turn the pokers(组合数,费马小定理)
- 【数论】HDU 4869 Turn the pokers 费马小定理
- hdu 4704 Sum||hdu 4869 Turn the pokers 费马小定理
- HDU 4869 Turn the pokers(瞎搞+逆元快速幂)
- HDOJ--4869--Turn the pokers【组合数学+快速幂】
- HDU4869 组合数学+费马小定理求逆元+快速幂
- hdu4869 费马小+快速幂
- hdoj 4869 Turn the pokers 【组合数 + 逆元】【费马小定理——>逆元】
- HDU 4869 Turn the pokers(思维+组合公式+快速幂)
- HDU 4869 Turn the pokers
- HUD 4869 Turn the pokers
- Web.xml配置详解
- C#开发active插件!
- Declaring Variables in Assembly Language
- python中为list中含list元素指定偏移key量及次偏移量的排序算法
- Picture control显示图像及Mat转换为CImage
- HDU4869:Turn the pokers(费马小定理+快速幂)
- 用Dancing Links求解精确覆盖问题
- 杭电 acm 2009
- OJ J
- java中gc的整理
- Mayavi
- CopyOnWriteArrayList源码阅读
- ros路由器cpu占用率高的原因和解决
- POJ 2540 半平面交