<模板>Hdu4869 Turn the pokers 组合数求余 费马小定理

37 篇文章 0 订阅
34 篇文章 0 订阅

Turn the pokers

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


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次操作,每次翻转xi张牌,问最后有多少种情况。

题解:不论前面怎么翻转,我们只需要知道最后牌的状态是哪几种,然后求组合数就可以了,比如有6张牌,两张向上,或四张向上。这里有个很神奇的地方,比赛的时候没有想到,最后的结果只需要知道最少是多少张朝上,最多有多少张朝上,他们的状态都是同奇或者同偶。比如最少1张向上,那么他们其他状态一定是1+2i张向上的,所有我们只需要左右区间即可。不信你自己模拟翻一翻牌就知道了。然后就是我们得仔细考虑怎么求最小最大了。这个多YY多推敲,万一不行就实践试试总会搞出来的。so我们知道最后牌的几种状态k=[L,R](位置可以不同),所以接下来就需要求这些状态的组合数了C(M,k)。这里数据好像很大的样子。我们可以考虑用这个不记得是费马小定理还是什么东西。(a/b)%M=a*(b^(M-2))%M。我们可以把组合数C(M,i)==C(M,i-1)*(M-i+1)/i 这个关系.套用左边那个公式。递推求得所有C(M,X)的值,然后找到区间[L,R]的组合数相加,即是最后的解。

增加一个链接:http://blog.csdn.net/modiz/article/details/38257837

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
//#define DEBUG
const LL M=1000000009;
LL c[100110];
LL ksm(LL a,LL b)
{
	LL ans=1;
	while (b)
	{
		if (b&1)
			ans=ans* a % M;
		b>>=1;
		a=a*a%M;
	}
	return ans;
}
int main()
{
	LL n,m;
//	freopen("1009.in","r",stdin);
	while (~scanf("%I64d%I64d",&n,&m))
	{
		LL l=0,r=0;
		for (int i=1;i<=n;i++)
		{
			LL ll,rr,x;
			scanf("%I64d",&x);
			if (l>=x) ll=l-x;
			else if (r>=x) ll=(l+x)&1;
			else ll=x-r;
			if (r+x<=m) rr=r+x;
			else if (l+x<=m) rr=m-((m+l+x)&1);
			else rr=2*m-l-x;
			l=ll;
			r=rr;
		}
		c[0]=1;
	//	cout<<l<<" "<<r<<endl;
		for (int i=1;i<=m;i++)
			if (m-i<i) c[i]=c[m-i];
			else c[i]=((c[i-1]*(m-i+1))%M * ksm(i,M-2))%M;
		LL ans=0;
		for (int i=l;i<=r;i+=2)
			ans=(ans+c[i]) % M;
		cout<<ans<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值