题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869

Turn the pokers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2001    Accepted Submission(s): 707

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)

 
Author
FZU
 
Source
完全学习的http://blog.csdn.net/libin56842/article/details/38065951,感谢
通过这个学习了快速幂和费马小定理的应用
一、快速幂:
  对于a^n可以用分治的思想令a^n = a^(n/2)*a^(n/2) 注意要分奇偶。
  一种直观的用递归表示的方法如下:
    

 LL quickmod(LL a,LL b)
{
LL ans = ;
if(b==) return ans;
if(b&) ans = (ans*a)%mod;
return ans = (quickmod(a,b/))%mod;
})%mod;

  为了降低复杂度。我们现在展开这个递归式子,写一个非递归的程序:

  

1.如果b是偶数,我们可以记k = a2 mod c,那么求(k)b/2 mod c就可以了。

2.如果b是奇数,我们也可以记k = a2 mod c,那么求

((k)b/2 mod c × a ) mod c =((k)b/2 mod c * a) mod c 就可以了。

上述过程最后一定是b先等于1再等于0,故b=0时候结束程序

  下面是模板代码:

 LL quickmod(LL a,LL b)
{
LL ans = ;
while(b)
{
if(b&) ans = (ans*a)%mod;
a = (a*a)%mod;
b>>=;
}
return ans;
}

下面介绍一下费马小定理:

  a^(p-1) = 1(mod p)  p是素数

  一般应用有:

A: a^b mod p 在b很大的时候可以先用b = b % (p-1)

B:   在阶乘中减去除法的操作。a^(p-2) = 1/a(mod p)

这个题就应用了B

下面是这个题的ac代码:

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
const LL mod = ;
const int N = ; LL f[N]; void init()
{
int i;
f[] = ;
for(i = ; i < N; i++)
f[i] = (f[i-]*i)%mod;
} LL quickmod(LL a, LL b)
{
LL ans = ;
while(b)
{
if(b&){
ans = (ans*a)%mod;
}
b>>=;
a = (a*a)%mod;
}
return ans;
} int main()
{
int n,m,i,j,k,l,r,x,ll,rr;//ll保存最小的1的个数
//l表示上一次的最小的1的个数,rr保存的是最多的1的个数
//r表示的是上一次的最多的1的个数
init();
while(~scanf("%d%d",&n,&m))
{
l = r = ;
for( i = ; i < n; i++)
{
scanf("%d",&x);
if(l>=x) ll = l - x;
else if(r>=x) ll = ((l%)==(x%))?:;
else ll = x-r; if(r+x<=m) rr = r+x;
else if(l+x<=m) rr = (((l+x)%)==(m%)?m:m-);
else rr = *m-(l+x); l = ll;
r = rr;
}
LL sum = ;
for(i = l; i<=r; i+=)
sum+=((f[m]%mod)*(quickmod((f[i]*f[m-i])%mod,mod-)%mod))%mod;
printf("%I64d\n",sum%mod);
}
return ;
}
 #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[] = ;
for(i = ; i<maxn; i++)
f[i] = (f[i-]*i)%mod;
} LL quickmod(LL a,LL b)
{
LL ans = ;
while(b)
{
if(b&)
{
ans = (ans*a)%mod;
b--;
}
b/=;
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 = ;
for(i = ; 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%)==(x%))?:;//当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)%) == (m%)?m:m-);//在r+x>m但是l+x<=m的情况下,也是判断奇偶,同态那么必定在中间有一种能全部变为1,否则至少有一张必定为0
else rr = *m-(l+x);//在l+x>m的情况下,等于我首先把m个1变为了0,那么我还要翻(l+x-m)张,所以最终得到m-(l+x-m)个1 l = ll,r = rr;
}
LL sum = ;
for(i = l; i<=r; i+=)//使用费马小定理和快速幂的方法求和
sum+=((f[m]%mod)*(quickmod((f[i]*f[m-i])%mod,mod-)%mod))%mod;
printf("%I64d\n",sum%mod);
} return ;
}

hdu_4869(费马小定理+快速幂)的更多相关文章

  1. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

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

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

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

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的 ...

  4. hdu4549(费马小定理 + 快速幂)

    M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n ...

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

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description   Sample Input 2 Sample Outp ...

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

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. 牛客训练四:Applese 涂颜色(费马小定理+快速幂)

    题目链接:传送门 思路: 考虑每一列有2种颜色,总共有n行,每一行的第一个格确定颜色,由于左右颜色不相同,后面的行就确定了. 所以总共有2^n中结果. 由于n太大,所以要用到费马小定理a^n%mod= ...

  8. hdu 4704 sum(费马小定理+快速幂)

    题意: 这题意看了很久.. s(k)表示的是把n分成k个正整数的和,有多少种分法. 例如: n=4时, s(1)=1     4 s(2)=3     1,3      3,1       2,2 s ...

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

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. C 函数参数 char **s与char *s[]

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/126 先来看一个小例子 : 编写函数遍历一个整型数组的元素,数组 ...

  2. iOS学习——iOS常用的存储方式

    不管是在iOS还是Android开发过程中,我们都经常性地需要存储一些状态和数据,比如用户对于App的相关设置.需要在本地缓存的数据等等.根据要存储的的数据的大小.存储性质以及存储类型,在iOS和An ...

  3. python 中一些关键字的区别

    一.raw_input 和input input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的 1.当输入为纯数字时 input返回的是数值类型, ...

  4. CSS3媒体查询(Media Queries)介绍

    媒体类型 all 所有设备 screen 电脑显示器 handheld 便携设备 tv 电视类型设备 print 打印用纸打印预览视图 关键字 and not(排除某种设备) only(限定某种设备) ...

  5. SQL SERVER 常用知识整理

    以前写了一些关于sql的文章,包括一些转载的,这里做下整理,方便需要时候使用 一.基础运用 SQL 数据结构操作语句 SQL 时间处理 SQL 常见函数使用 CASE WHEN THEN 小结 二.优 ...

  6. Spring之Bean的基本概念

     转自http://blog.csdn.net/chenssy/article/details/8222744 Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于 ...

  7. jQuery 实现无限任意添加下拉菜单

    新学jQuery还有很多没学,今天做了个下拉菜单,按照自己的思想结合学的基础效果实现一款可以任意添加层数的下拉菜单,如果有什么建议,欢迎指教啦啦啦 我喜欢备注细一些,这样给自己也是一种理解和方便回顾哈 ...

  8. WebRTC 入门到放弃(一)WebRTC

    前言 WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的技术,是谷歌2010年以6820万美元收购Gl ...

  9. 3.移植uboot-使板卡支持nor、nand

    在上一章,我们添加了nor,nand启动后,uboot启动出如下图所示: 上面的Flash: *** failed *** 是属于uboot第二阶段函数board_init_r()里的代码, 代码如下 ...

  10. Jenkins远程部署SpringBoot应用

    一般Web工程通过Jenkins远程部署到Tomcat,可以采用Maven的tomcat-maven-plugin插件进行部署.最近接触到Spring Boot工程的部署,由于Spring Boot应 ...