hdu5446 Unknown Treasure(数论综合题:大组合数取大合数模:Lucas+CRT)

171 篇文章 0 订阅
79 篇文章 0 订阅


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5446

Unknown Treasure

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1590    Accepted Submission(s): 585


Problem Description
On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick  m  different apples among  n  of them and modulo it with  M M is the product of several different primes.
 

Input
On the first line there is an integer  T(T20)  representing the number of test cases.

Each test case starts with three integers  n,m,k(1mn1018,1k10)  on a line where  k  is the number of primes. Following on the next line are  k different primes  p1,...,pk . It is guaranteed that  M=p1p2pk1018  and  pi105  for every  i{1,...,k} .
 

Output
For each test case output the correct combination on a line.
 

Sample Input
  
  
1 9 5 2 3 5
 

Sample Output
  
  
6
 

Source
 


题意:求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数。

AC code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#define LL long long
#define MAXN 1000010
using namespace std;
const  int N=20;//模方程数 
LL a[N],mod[N];
LL mul(LL a,LL b,LL mod)//a*b%mod
{
	LL ans=0;
	while(b){
		if(b&1)
			ans=(ans+a)%mod;
		b>>=1;
		a=(a+a)%mod;
	}
	return ans;
}

LL quick_mod(LL a,LL b,LL m)//a^b%m 
{
	LL ans=1;
	a%=m;
	while(b)
	{
		if(b&1)
		{
			ans=ans*a%m;
		}			
		b>>=1;
		a=a*a%m;
	}
	return ans;
}

LL getC(LL n,LL m,int cur)//C(n,m)%mod[cur]
{
	LL p=mod[cur];
	if(m>n)
		return 0;
	if(m>n-m)
		m=n-m;
	LL ans=1;
	for(int i=1;i<=m;i++)
	{
		LL a=(n+i-m)%p;
		LL b=i%p;
		//ans=mul(ans,mul(a,quick_mod(b,p-2,p),p),p);//p为素数,i对p的逆元可以不用扩张欧几里得进行求解  re=i^(P-2) 
		ans = ans * (a * quick_mod(b, p-2,p) % p) % p;  
	}
	return ans; 
}

LL Lucas(LL n,LL k,int cur)//求C(n,k)%mod[cur] 
{
	LL p=mod[cur];
	if(k==0)
		return 1%p;
	//return getC(n%p,k%p,cur)*Lucas(n/p,k/p,cur)%p;
	return getC(n % p, k % p,cur) * Lucas(n / p, k / p,cur) % p;  
}

void extend_Euclid(LL a,LL b,LL &x,LL &y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return;
	}
	extend_Euclid(b,a%b,x,y);
	LL tmp=x;
	x=y;
	y=tmp-a/b*y;
}

LL CRT(LL a[],LL m[],int k)//求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数,则先用a[i]存储模方程C(n,m)%mi,
{                           //m[]存储所有素数因子mi,k表示总共有k个模方程,返回C(n,m)%M的值 
	LL M=1;
	LL ans=0;
	for(int i=0;i<k;i++)
		M*=mod[i];
	for(int i=0;i<k;i++)
	{
		LL x,y,tmp;
		LL Mi=M/m[i];
		extend_Euclid(Mi,m[i],x,y);
		if(x<0)
		{
			x=-x;
			tmp=mul(Mi,x,M);
			tmp=mul(tmp,a[i],M);
			tmp=-tmp;
		}
		else
		{
			tmp=mul(Mi,x,M);
			tmp=mul(tmp,a[i],M);
		}
		ans=(ans+tmp)%M;
	}
	while(ans<0)
		ans+=M;
	return ans;
}

int main()
{
	//freopen("D:\\in.txt","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--)
	{
		LL n,m;
		int k;
		scanf("%lld%lld%d",&n,&m,&k);
		//k=1;
		for(int i=0;i<k;i++)
			scanf("%lld",&mod[i]);
		for(int i=0;i<k;i++)
			a[i]=Lucas(n,m,i)%mod[i];
		//printf("%I64d\n",a[0]);
		printf("%lld\n",CRT(a,mod,k)); 
	}
  	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值