Unknown Treasure HDU - 5446 (Lucas定理+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 mm different apples among nn of them and modulo it with MM. MM is the product of several different primes.
Input
On the first line there is an integer T(T20) T ( T ≤ 20 ) representing the number of test cases.

Each test case starts with three integers n,m,k(1mn1018,1k10) n , m , k ( 1 ≤ m ≤ n ≤ 10 18 , 1 ≤ k ≤ 10 ) on a line where k is the number of primes. Following on the next line are kk different primes p1,...,pk p 1 , . . . , p k . It is guaranteed that M=p1p2pk1018 M = p 1 · p 2 · · · p k ≤ 10 18 and pi105 p i ≤ 10 5 for every i1,...,k 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
2018.8.24重新更新了代码,对于有多个不同质数取模,我换了一种风格来写
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ull  unsigned long long
#define ll long long
#define maxn 100005
using namespace std;
ll mod;
ll fac[maxn];
inline ll mul(ll x,ll y){
    x=x%mod,y=y%mod;
    return ((x*y-(ll)(((long double)x*y+0.5)/mod)*mod)%mod+mod)%mod;
}
ll P(ll a,ll b)
{
    ll ans=1;
    a%=mod;
    while(b)
    {
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
inline void init()
{
    fac[0]=1;
    for(int i=1;i<mod;i++)
        fac[i]=fac[i-1]*i%mod;
}
inline ll C(ll _n,ll _m)
{
    if(_n<_m)return 0;
    return fac[_n]*P(fac[_n-_m]*fac[_m]%mod,mod-2)%mod;
}
inline ll Lucas(ll _n,ll _m)
{
    if(!_m)return 1;
    return Lucas(_n/mod,_m/mod)*C(_n%mod,_m%mod)%mod;
}
ll k;
ll p[15];
ll r[15];
ll CRT()
{
    ll ans=0;
    ll M=1;
    for(int i=0;i<k;i++)M*=p[i];
    for(int i=0;i<k;i++)
    {
        mod=p[i];
        ll Mi=M/p[i];
        ll ti=P(Mi,mod-2);
        mod=M;
        ll temp=mul(mul(Mi,ti),r[i]);
        ans=(ans+temp)%M;
    }
    return ans;
}
int main()
{
    int t;
    ll n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>k;
        for(int i=0;i<k;i++)
            scanf("%lld",p+i);
        for(int i=0;i<k;i++)
        {
            mod=p[i];
            init();
            r[i]=Lucas(n,m);
        }
        cout<<CRT()<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值