HDU4869:Turn the pokers(費馬小定理+快速冪)
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
#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