[HDU](5446)Unknown Treasure ---- 组合数学(卢卡斯定理)+CRT

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(T≤20) representing the number of test cases.

Each test case starts with three integers n,m,k(1≤m≤n≤1e18,1≤k≤10) 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=p1⋅p2⋅⋅⋅pk≤1e18 and pi≤1e5 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

题意: 给你n,m,k,k个数p1,p2……pk
让你求C(n,m)%(p1*p2*p3*……*pk)

1≤m≤n≤1e18 这样子无用普通的组合数,需要用卢卡斯定理

我们先求处r[i] = C(n,m)%p[i]
然后在用一步中国剩余定理就可以求得结果了

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,e)      for(int i=s;i<=e;i++)
#define rev(i,s,e)      for(int i=e;i>=s;i--)
#define all(x)          x.begin(),x.end()
#define sz(x)           x.size()
#define szz(x)          int(x.size()-1)
const int INF = 0x3f3f3f3f;
const int MOD =  1e9+7;
const int MAXN = 2e5+10;
typedef long long LL;
typedef pair<LL,LL> PII;
LL qmul(LL a,LL p,LL m)
{
    LL tmp = 0;
    while(p)
    {
        if(1&p) tmp = (tmp+a)%m;
        a = (a+a)%m;
        p>>=1;
    }
    return tmp;
}
void exgcd(LL a,LL b,LL &x,LL &y,LL &d)
{
    if(!b) d=a,x = 1,y=0;
    else exgcd(b,a%b,y,x,d),y-=(a/b)*x;
}
LL inv(LL a,LL p)
{
    LL x,y,d;
    exgcd(a,p,x,y,d);
    return d==1?(x+p)%p:-1;
}
LL fat(LL x,LL p)
{
    LL ans = 1;
    rep(i,2,x) ans = (ans*i)%p;
    return ans;
}
LL c(LL n,LL m,LL p)
{
    if (m < 0 || m > n) return 0; //组合数注意处理这种情况
    return fat(n,p)*inv(fat(m,p),p)%p*inv(fat(n-m,p),p)%p;
}
LL crt(LL *a,LL *m,int n)//借助CRT求c(n,m)%M
{
    LL M = 1,res = 0;
    rep(i,0,n-1) M*=m[i];
    rep(i,0,n-1)
    {
        LL w = M/m[i];
        res = (res+qmul(w*inv(w,m[i]),a[i],M))%M;//这里要用快速乘防止溢出,否则会WA
    }
    return (res+M)%M;
}
LL Lucas(LL n,LL m,LL p)
{
    return m?Lucas(n/p,m/p,p)*c(n%p,m%p,p)%p:1;
}
int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    LL n,m;
    LL p[15];
    LL a[15];
    int t,k;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>k;
        rep(i,0,k-1) cin>>p[i];
        rep(i,0,k-1) a[i] = Lucas(n,m,p[i]);
        cout<<crt(a,p,k)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值