HDU4869:Turn the pokers(费马小定理+高速幂)
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?
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).
3 4
3 2 3
3 3
3 2 3
8
3HintFor 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)
#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(费马小定理+高速幂)的更多相关文章
- 2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)
题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少 ...
- hdu_4869(费马小定理+快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 Turn the pokers Time Limit: 2000/1000 MS (Java/O ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
- BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的 ...
- 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 ...
- HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Outp ...
- hdu1576-A/B-(同余定理+乘法逆元+费马小定理+快速幂)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 牛客训练四:Applese 涂颜色(费马小定理+快速幂)
题目链接:传送门 思路: 考虑每一列有2种颜色,总共有n行,每一行的第一个格确定颜色,由于左右颜色不相同,后面的行就确定了. 所以总共有2^n中结果. 由于n太大,所以要用到费马小定理a^n%mod= ...
- 【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies
G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the pro ...
随机推荐
- block 解析 - 形参变量
block形参 之前漏了一篇block形参的介绍,这里给补上. block形参就是定义block带的参数,和函数的参数使用一样,我们可以在block随意使用修改block形参. 我们来看个例子: 我们 ...
- cxf调用c#的webservice
java调用c#的webservice,如今已经測试通过.并且用到了项目中. 如今把实现方式和遇到的问题分享给大家.详细源代码例如以下: JaxWsDynamicClientFactory dcf = ...
- java io学习记录(路径分隔符)
java路径分隔符(路径表示) path="E:\\xp\\test\\2.jpg"; path="E:/xp/test/2.jpg"; path=" ...
- Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
今天布置环境的时候发现一个问题:Your working copy is out of date. Try pulling from the remote to get the latest chan ...
- C#中对文件的操作
详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801 源码举例: public class FileSystemManager { ...
- 如何使用springmvc的@requestbody 返回json数据
先配置 XXX_ servletxml <!-- 整合jackson 返回一个json格式 --><bean class="org.springframework.web. ...
- POJ 2187 Beauty Contest 凸包
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27276 Accepted: 8432 D ...
- hdoj 1286 找新朋友 【数论之欧拉函数】
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 11g r2 模拟OCR和voting disk不可用,完整恢复过程,以及一些注意事项
环境:RHEL5.8 RAC 11.2.0.3.0 1:查看ORC和voting disk信息: In 11g Release 2 your voting disk data is automatic ...
- CentOS6.4 安装 Oracle11g
1.硬件要求检查: 1.1 内存要求: 内存大于1G(使用虚拟机安装时内存要稍微大一些,否则安装检查不通过) #cat /proc/meminfo //查看内存大小 1.2 交换分区要求: 交换分区是 ...