HDU5446(2015年长春网络赛1010)改

HDU5446(2015年长春网络赛1010)

题目我就不列举了。。简单地说就是求C(n,m)%M 
明显就是Lucas+中国剩余定理 

无限WA就是long long * long long %m会爆。所以要处理一下


#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<vector>
#define Mn 200005
#define Me 1005
#define chn 26
#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define drep(i, r, l) for (int i = (r); i >= (l); --i)
using namespace std;
typedef long long LL;
LL n,m,k;
LL p[15],mod[15];
LL quick_mod(LL a, LL b,LL p)
{
    LL ans = 1;
    a %= p;
    while(b)
    {
        if(b & 1)
        {
            ans = ans * a % p;
        }
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}
LL C(LL n, LL m,LL p)
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % p;
        LL b = i % p;
        ans = ans * (a* (quick_mod(b, p-2,p) % p)) % p;
    }
    return ans;
}
LL Lucas(LL n, LL m,LL p)
{
    if(m == 0) return 1;
    return C(n % p, m % p,p) * (Lucas(n / p, m / p,p) % p)%p;
}

// 中国剩余定理
LL Extended_Euclid(LL a, LL b, LL &x, LL &y) {
    LL d;
    if(b == 0) {
        x = 1; y = 0;
        return a;
    }
    d = Extended_Euclid(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
LL mod_mul(LL a, LL b, LL mod) {
    LL ret = 0;
    while(b) {
        if(b & 1)    ret = (ret + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return ret;
}
LL china(LL k,LL *a,LL *mm) {
    LL d, x, y, tmp, M, ret;
    ret = 0;
    M = 1;
    for(int i = 0; i < k; i++)
        M *= mm[i];
    for(int i = 0; i < k; i++) {
        tmp = M / mm[i];
        d = Extended_Euclid(mm[i], tmp, x, y);
        LL temp = mod_mul(y, tmp, M);
        temp = mod_mul(temp, a[i], M);
        ret = (ret + temp) % M;
        //ret=(ret+y*tmp*a[i])%M;
    }
    return (M + ret) % M;
}
int main()
{
#ifndef ONLINE_JUDGE
        freopen("1.cpp", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d", &n, &m, &k);
        int i;
        for(i=0;i<k;i++) scanf("%I64d",&p[i]);
        for(i=0;i<k;i++) mod[i]=Lucas(n,m,p[i]);
        printf("%I64d\n",china(k,mod,p));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值