首页 > 代码库 > 重复组合+lucas

重复组合+lucas

从n个元素中有重复地取r个,不计其顺序,则不同的取法有C(r,n+r-1)种。

 

lucas模版

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MOD 10007
#define LL long long
using namespace std;

LL qmod(LL a, LL b, LL c)
{
    LL ans = 1;
    a = a % c;
    while(b)
    {
        if(b&1) ans = (ans*a)%c;
        b = b>>1;
        a = (a*a)%c;
    }
    return ans;
}

LL c(LL m,LL n)
{
    if(m < n)   return 0;
    if(m == n)  return 1;
    if(n > m-n) n = m-n;
    LL mm = 1,nn = 1;
    for(LL i = 0;i < n;i++)
    {
        mm = mm*(m-i)%MOD;
        nn = nn*(n-i)%MOD;
    }
    return mm*qmod(nn,MOD-2,MOD)%MOD;
}

LL lucas(LL m,LL n)
{
    LL ans = 1;
    while(m && n && ans)
    {
        ans = ans%MOD*c(m%MOD,n%MOD)%MOD;
        n /= MOD;
        m /= MOD;
    }
    return ans;
}

int main()
{
    LL n,m;
    while(~scanf("%lld%lld",&m,&n))    printf("%lld\n",lucas(m+n-1,n));
    return 0;
}

//XDOJ1057 

 

重复组合+lucas