HDU 4869 (递推 组合数取模)
Problem Turn the pokers (HDU 4869)
题目大意
有m张牌,全为正面朝上。进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数。
解题分析
记正面朝上为1,朝下为0。
若最后有x个1,则对答案的贡献为C(n,x)。所以只需要知道最后可能的1的个数。
假设已经有a个1,某次操作可以将b张牌反面,可以发现操作之后可能的1的个数相差2。
记录每次操作后1的个数所在区间为[l ,r],即可能取到的个数为l , l+2 , l+4 , ...... , r 。
每次转移分类讨论一下,贪心转移即可。
参考程序
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; #define N 100008
#define mo 1000000009
int a[N];
int pa[N],pb[N];
int n,m; void calc(int &l,int &r,int x){
int ll,rr;
if (x<l) ll=l-x;
if (l<=x && x<=r) ll=(r-x) & ;
if (r<x) ll=x-r; if (x<m-r) rr=x+r;
if (m-r<=x && x<=m-l) rr=m-((m-l-x) & );
if (m-l<x) rr=m-(x-(m-l));
l=ll; r=rr;
} int C(int x,int y){
return 1ll*pa[x]*pb[y] % mo *pb[x-y] % mo;
} int quick_pow(int x,int y){
int res=;
while (y){
if (y&) res=1ll*res*x % mo;
x=1ll*x*x % mo;
y=y >>;
}
return res;
} int main(){
pa[]=pb[]=;
for (int i=;i<N;i++) pa[i]=(1ll*pa[i-]*i) % mo;
for (int i=;i<N;i++) pb[i]=quick_pow(pa[i],mo-);
while (~scanf("%d %d",&n,&m)){
for (int i=;i<=n;i++) scanf("%d",&a[i]);
int l=,r=;
for (int i=;i<=n;i++) calc(l,r,a[i]); long long ans=;
for (int i=l;i<=r;i+=) ans= (ans+C(m,i)) % mo;
printf("%I64d\n",ans);
}
}
HDU 4869 (递推 组合数取模)的更多相关文章
- cf166e 在四面体上寻找路线数 递推,取模
来源:codeforces E. Tetrahedron You are given a tetrahedron. Let's mark its vertices ...
- 洛谷P4723 【模板】线性递推(多项式取模 线性代数)
题意 题目链接 Sol Orz shadowice 注意,下面的代码自带O(随时TLE)倍大常数.. #include<bits/stdc++.h> #define Pair pair&l ...
- 排列组合+组合数取模 HDU 5894
// 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- Uva12034 (组合数取模)
题意:两匹马比赛有三种比赛结果,n匹马比赛的所有可能结果总数 解法: 设答案是f[n],则假设第一名有i个人,有C(n,i)种可能,接下来还有f(n-i)种可能性,因此答案为 ΣC(n,i)f(n-i ...
- [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】
题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...
- lucas定理解决大组合数取模
LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...
- 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)
J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- Hibernate映射之实体映射
1.使用@注解配置实体类 实体类一般有ID.普通属性.集合属性等,分别对应数据库的主键.普通列.外键.@注解配置中,实体类用@Entity注解,用@Table指定对应的数据表,用@Id配置主键,用@C ...
- NPOI相关
总结一下工作中遇到的NPOI以及在ASP.NET MVC中的使用 http://www.cnblogs.com/fenglingyi/p/4750323.html
- plot a critical difference diagram , MATLAB code
plot a critical difference diagram , MATLAB code 建立criticaldifference函数 function cd = criticaldiffer ...
- How to decide on the correct number of clusters?
Determining the number of clusters/segments in hierarchical clustering/segmentation algorithms 由于uni ...
- 基于TCP协议的网络通信
TCP/IP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信.Java对基于TC ...
- hduacm 5104
http://acm.hdu.edu.cn/show #include <cstdio> #include <cstring> #include <algorithm&g ...
- struts2最新s2-016代码执行漏洞CVE-2013-2251
这是一个代码执行漏洞,利用java代码来执行系统命令. 影响版本:Struts 2.0.0 – Struts 2.3.15 漏洞说明: The Struts 2 DefaultActionMa ...
- iOS 文件读写
#import <Foundation/Foundation.h> @interface Utils : NSObject +(void) writeFile:(NSString *) f ...
- Program C 暴力求解
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...
- 傅里叶变换:MP3、JPEG和Siri背后的数学
九年前,当我还坐在学校的物理数学课的课堂里时,我的老师为我们讲授了一种新方法,给我留下了深刻映像.我认为,毫不夸张地说,这是对数学理论发现最广泛的应用.应用的领域包括:量子物理.射电天文学.MP3和J ...