1. 程式人生 > >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, 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的個數,然後再這個區間內進行組合數的求解即可 但是又有一個問題出來了,資料很大,進行除法是一個不明智的選擇,但是組合數公式必定有除法 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;
}


相關推薦

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, te

HDU 4704 Sum(隔板原理+組合數求和公式+定理+快速

ace php 模板 erl char printf 證明 style ron 題目傳送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sam

定理+快速取模】ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies

print using pri long long ger ssi bit one ive G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To mak

定理+快速取模】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

定理+快速:ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies

限制時間是1S,試了試Java的大數運算,超時了。發現雖然 N 的值大的可怕但結果是取餘後的,可以通過費馬小定理減小指數大小後快速冪得到結果。 快速冪運算時必須加上取餘, (a * b) %

焦作2018網路賽_Give Candies(定理+快速)

There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more interesting, Miss Li comes up with the rul

[HDU 4704] Sum · 定理 & 快速

題意:給定n,設是將n分成k個數之和的方案數,求 隔板原理:將n個物品分成k組,相當於在n-1個間隔中插入k-1個隔板,方案數為,所以等於,貌似是叫二項式定理來著?反正這個式子的值等於,所以就是要求的

HDU 4704 Sum 【隔板原理+定理+快速

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2745    Accepted Submissi

HDU 4704 [定理+快速] ---狗眼不識多校

Description Input 2 Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file c

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 &l

HDU4704 定理+快速

容易看出來每次輸出的結果為,2的n-1次方對10e9+7取模,但是n太大了,這裡利用到了費馬小定理:a是整數,p是素數,且gcd(a,p) = 1,則有 a^(p-1) % p = 1 % p = 1.顯然這裡mod是素數並且與2互質,所以有2^(mod - 1) % mod

HDOJ 4704 Sum(定理+快速

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm

HDU 4704 Sum 定理+快速

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 18    Accepted Submission

hdu 4704 sum(定理+快速

題意:   這題意看了很久。。     s(k)表示的是把n分成k個正整數的和,有多少種分法。   例如: n=4時, s(1)=1     4   s(2)=3     1,3      3,1       2,2      s(3)=3     1,1,2     

定理 + 快速 Give Candies

There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more interesting, Miss Li comes up with the rul

hdu 5667 Sequence(矩陣快速+定理+快速)

#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #inc

4704 Sum (定理 + 快速

Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of mul

HDU 5201 The Monkey King(組合數學)(隔板法+容斥定理+定理)

逆元 cst 大於 ont amp space pro http strong http://acm.hdu.edu.cn/showproblem.php?pid=5201題意:給你n個桃子要你分給m只猴子,猴子可以得0個桃子,問有多少種方法,但是有一個限制條件: 第一只猴

Codeforces Problem 711E ZS and The Birthday Paradox(抽屜原理+乘法逆元+定理+組合數+快速+概率論)

ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% ch

hdu4549矩陣快速+定理

次方 pla pragma nod 技術分享 gif 矩陣 end eof 轉移矩陣很容易求就是|0 1|,第一項是|0| |1 1| |1| 然後直接矩陣快速冪,要用到費馬小定理 :假如p