HDU 3037 (大数&LUCAS定理)

题目大意:求在n棵树上摘不超过m颗豆子的方案,结果对p取模。

解题思路:

题目可以转换成  x1+x2+……+xn=m 有多少组解,m在题中可以取0~m。

利用插板法可以得出x1+x2+……+xn=m解的个数为C(n+m-1,m);

则题目解的个数可以转换成求   sum=C(n+m-1,0)+C(n+m-1,1)+C(n+m-1,2)……+C(n+m-1,m)

利用公式C(n,r)=C(n-1,r)+C(n-1,r-1)  == >  sum=C(n+m,m)。

现在就是要求C(n+m,m)%p。

因为n,m很大,这里可以直接套用Lucas定理的模板即可。

Lucas(n,m,p)=C(n%p,m%p,p)*Lucas(n/p,m/p,p);   ///这里可以采用对n分段递归求解,

Lucas(x,0,p)=1;

AC代码:(LUCAS函数可直接使用)


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <complex>
#include <cstdio>
using namespace std;
#define ll long long
#define mod 110119
//ll n,m,r,ca=0,cnt,a,b,rig,up;
#define N 111000

ll mod_pow(ll a,ll n,ll p)
{
    ll ret=1,A=a;
    while(n)
    {
        if (n & 1)
            ret=(ret*A)%p;
        A=(A*A)%p;
        n>>=1;
    }
    return ret;
}

ll factorial[N];

void init(ll p)
{
    factorial[0] = 1;
    for(int i = 1;i <= p;i++)
        factorial[i] = factorial[i-1]*i%p;
}

ll Lucas(ll a,ll k,ll p) //求C(n,m)%p p最大为10^5。a,b可以很大!  a个数中挑k个的组合数
{
    ll re = 1;
    while(a && k)
    {
        ll aa = a%p;ll bb = k%p;
        if(aa < bb) return 0; //这个是最后的改动!
        re = re*factorial[aa]*mod_pow(factorial[bb]*factorial[aa-bb]%p,p-2,p)%p;//这儿的求逆不可先处理
        a /= p;
        k /= p;
    }
    return re;
}


int main()
{

    int t;
    ll a,b,p;
    cin >> t;
    while(t--)
    {
        scanf("%I64d%I64d%I64d",&a,&b,&p);
        init(p);
        ll ans=Lucas(a+b,b,p);
        printf("%I64d\n",ans);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值