HDU 3037 Saving Beans

/*
hdu3037
http://acm.hdu.edu.cn/showproblem.php?pid=3037
lucas 模板题
*/

#include <cstdio>
#include <cmath>
const long long Nmax=100005;
long long p;

long long ex_gcd(long long a,long long b,long long &x,long long &y)//solve x,y in a*x+b*y=ex_gcd(a,b,x,y)=gcd(a,b);
{
    if(b==0)
    {
        x=1LL;
        y=0LL;
        return a;
    }
    long long ans=ex_gcd(b,a%b,x,y);
    long long tmp=x;
    x=y;
    y=tmp-a/b*y;
    return ans;
    //x = x0 + (b/gcd)*t
    //y = y0 – (a/gcd)*t

}

long long get(long long a,long long m,long long c)//get x in a*x=c(mod m)
{
    //we can solve x,y in a*x+b*y=c <=> c%gcd(a,b)==0
    long long x,y;
    long long gcd=ex_gcd(a,m,x,y);
    if(c%gcd!=0)
        return -1;//error
    x*=c/gcd;
    if(m<0)
        m=-m;
    long long ans=x%m;
    while(ans<0)
        ans+=m;
    return ans;
}

// long long Cm(long long n,long long m,long long p)//C(n,m)=n!/(m!(n-m)!);
// {
//     long long ans=1LL;
//     ans=fac(n)%p;
//     ans=(ans* (get(fac(m)*fac(n-m),p,1)%p) )%p;
//     return ans;
// }

long long Cm(long long n,long long m,long long p)
{
    long long a=1,b=1;
    if(n<m)
        return 0LL;
    while(m>0)
    {
        a=(a*n)%p;
        b=(b*m)%p;
        n--;
        m--;
    }
    return a*get(b,p,1)%p;
}

//Lucas C(n*p+r,m*p+t)=C(r,t)*Lucas(n,m);
long long lucas(long long n,long long m,long long p) //求C(n,m)%p p最大为10^5。a,b可以很大!
{
    if(m==0)
        return 1;
    else
        return ( (Cm(n%p,m%p,p) %p ) * ( lucas(n/p,m/p,p) %p ) )%p;
}

int main()
{
    // freopen("HDU3037.in","r",stdin);
    long long t;
    long long n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld",&n,&m,&p);
        printf("%lld\n",lucas(n+m,m,p));
    }
    return 0;
}
时间: 2025-01-04 01:10:01

HDU 3037 Saving Beans的相关文章

HDU 3037 Saving Beans (Lucas定理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p, 用Lucas定理求大组合数取模的值 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int t; long long n, m, p; long long pow(long long n, long lo

hdu 3037 Saving Beans(组合数学)

hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以用到Lucas定理. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; ll n, m, p; ll qPow (ll a

HDU 3037 Saving Beans(lucas定理)

题目大意:豆子数i (1~m)分到n颗树上.  树可以为空,那么对于每个i,分配方式是 C(n+i-1,n-1)......于是我用for(i=0-->m)做,不幸超时,m太大. 不过竟然公式可以化简: for(int i=0;i<=m;i++) C(n+i-1,n-1)=C(n+i-1,i) 组合原理: 公式 C(n,k) = C(n-1,k)+C(n-1,k-1) C(n-1,0)+C(n,1)+...+C(n+m-1,m) = C(n,0)+C(n,1)+C(n+1,2)+...+C(n

hdu 3037 Saving Beans 组合数取模模板题。。

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2707    Accepted Submission(s): 1014 Problem Description Although winter is far away, squirrels have to work day and night to save b

[ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)

Saving Beans Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a probl

HDU 3037 Saving Beans (Lucas法则)

主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理求大组合数取模的值 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int t; long long n, m, p; long long pow(lo

HDU 3037 Saving Beans 多重集合的结合 lucas定理

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3037 题目描述: 要求求x1 + x2 + x3 + ...... + xn <= m 非负整数解的个数, 结果对P取模, 输入的变量是n, m, p, P一定是素数 解题思路: x1 + ... + xn = m 非负整数解的个数是C(n+m-1, n) , 所以答案就是 C(n+0-1, 0) + C(n+1-1, 1) + ...... C(n+m-1, n) 对P取模, 由于组合数公式C(

HDU 3037 Saving Beans(Lucas定理模板题)

Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They supp

HDU 3037 Saving Beans (数论,Lucas定理)

题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后就求这个值,直接求肯定不好求,所以我们可以运用Lucas定理,来分解这个组合数,也就是Lucas(n,m,p)=C(n%p,m%p)* Lucas(n/p,m/p,p). 然后再根据费马小定理就能做了. 代码如下: 第一种: #pragma comment(linker, "/STACK:10240