码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5446 Unknown Treasure 中国剩余定理+lucas

时间:2016-07-31 11:27:30      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

 

求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数。

先求出C(n, m)%pi的值, 然后这就是一个同余的式子。 用中国剩余定理求解。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
ll a[17], b[17];
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 mul(ll a, ll n, ll mod)
{
    a = (a%mod+mod)%mod;
    n = (n%mod+mod)%mod;
    ll ret = 0;
    while(n) {
        if(n&1)
            ret = (ret+a)%mod;
        a = (a+a)%mod;
        n >>= 1;
    }
    return ret;
}
ll CRT(ll a[],ll m[],int n)
{
    ll M = 1;
    ll ans = 0;
    for(int i=1; i<=n; i++)
        M *= m[i];
    for(int i=1; i<=n; i++)
    {
        ll x, y;
        ll Mi = M / m[i];
        extend_Euclid(Mi, m[i], x, y);
        ans = (ans + mul(mul(Mi, x, M), a[i], M))%M;
    }
    return (ans+M)%M;
}
ll pow(ll a, ll b, ll mod)
{
    ll ret = 1;
    while(b) {
        if(b&1) ret = ret*a%mod;
        a = a*a%mod;
        b /= 2;
    }
    return ret;
}
ll C(ll n, ll m, ll mod)
{
    ll a = 1, b = 1;
    for(int i = 1; i <= m; i++) {
        b = b*i%mod;
        a = a*(n-i+1)%mod;
    }
    return a*pow(b, mod-2, mod)%mod;
}
ll lucas(ll n, ll m, ll mod)
{
    if(m == 0)
        return 1;
    return lucas(n/mod, m/mod, mod)*C(n%mod, m%mod, mod)%mod;
}
int main()
{
    ll t, n, m, k;
    cin>>t;
    while(t--) {
        cin>>n>>m>>k;
        for(int i = 1; i <= k; i++) {
            scanf("%lld", &a[i]);
        }
        for(int i = 1; i <= k; i++) {
            b[i] = lucas(n, m, a[i]);
        }
        ll ans = CRT(b, a, k);
        cout<<ans<<endl;
    }
    return 0;
}

 

hdu 5446 Unknown Treasure 中国剩余定理+lucas

标签:

原文地址:http://www.cnblogs.com/yohaha/p/5722439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!