HDU4869:Turn the pokers(费马小定理+高速幂)

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张。非常easy想到组合数,可是关键是怎么进行组合数计算呢?我们能够发现,在牌数固定的情况下。总共进行了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+5

LL 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;
}
时间: 2024-11-16 04:15:25

HDU4869:Turn the pokers(费马小定理+高速幂)的相关文章

HDU4869:Turn the pokers(费马小定理+快速幂)

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,

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

题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少时最少的个数,rb代表1最多时的个数.一张牌翻两次和两张牌翻一次 得到的奇偶性相同,所以结果中lb和最多的rb的奇偶性相同.如果找到了lb和rb,那么,介于这两个数之间且与这两个数奇偶性相同的数均可取到,然后在这个区间内求组合数相加(若lb=3,rb=7,则3,5,7这些情况都能取到,也就是说最后的

hdu 4704 Sum (费马小定理+快速幂)

//(2^n-1)%mod //费马小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) # include <stdio.h> # include <algorithm> # include <string.h> # define mod 1000000007 using namespace std; __int64 pow(__int64 n) { __int64 p=1,q=2; while(n) { if(n%

hdu 4704 费马小定理+快速幂

题意就是:做整数拆分,答案是2^(n-1) 由费马小定理可得:2^n % p = 2^[ n % (p-1) ]  % p 当n为超大数时,对其每个数位的数分开来加权计算 当n为整型类型时,用快速幂的方法求解 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const in

BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的染色方案视为等价的,求等价类计数. 分析 给出置换求等价类计数,用Burnside引理:等价类计数=(每一个置换不动点的和)/置换数.(不知道的建议去看白书) 其中不动点是指一个染色方案经过置换以后染色与之前完全相同. 1.求不动点个数. 不动点的话同一个循环内的每一个点的颜色必须相同(否则不同颜色

HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 题意是输入一个N,求N被分成1个数的结果+被分成2个数的结果+...+被分成N个数的结果,N很大 1.隔板原理 1~N有

hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Holion August will eat every thing he has found. Now there are many foods,but he does not want to eat all of them at once,so he fi

【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N) and each time a child is inv

hdu1576-A/B-(同余定理+乘法逆元+费马小定理+快速幂)

A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10383    Accepted Submission(s): 8302 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个