1. 程式人生 > >【HDU 3037】大數組合取模之Lucas定理+擴充套件歐幾里得求逆元與不定方程一類問題

【HDU 3037】大數組合取模之Lucas定理+擴充套件歐幾里得求逆元與不定方程一類問題

Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2284    Accepted Submission(s): 828


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 suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees.

Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
Input The first line contains one integer T, means the number of cases.

Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
Output You should output the answer modulo p.
Sample Input 2 1 2 5 2 1 5
Sample Output 3 3 Hint
Hint For sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2 … and so on. The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are: put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.
Source

解題思路:

題目可以轉換成  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;

將n,m分解變小之後問題又轉換成了求(a/b)%p。

(a/b)%p可以轉換成a*Inv(b,p)  Inv(b,p)為b對p的逆元。

關於這個方程,引用一下論文



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long LL;
LL  n, m, p;

LL Ext_gcd(LL a, LL b, LL &x, LL &y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    LL ret = Ext_gcd(b, a % b, y, x);
    y -= a / b * x;
    return ret;
}
LL Inv(LL a, int m)   ///求逆元a相對於m
{
    LL d, x, y, t = (LL)m;
    d = Ext_gcd(a, t, x, y);
    if (d == 1) return (x % t + t) % t;
    return -1;
}

LL Cm(LL n, LL m, LL p)  ///組合數學
{
    LL a = 1, b = 1;
    if (m > n) return 0;
    while (m)
    {
        a = (a * n) % p;
        b = (b * m) % p;
        m--;
        n--;
    }
    return (LL)a * Inv(b, p) % p; ///(a/b)%p 等價於 a*(b,p)的逆元
}
// Lucas(n,m,p)=C(n%p,m%p,p)*Lucas(n/p,m/p,p);   ///這裡可以採用對n分段遞迴求解,
// Lucas(x,0,p)=1;

int Lucas(LL n, LL m, LL p)  ///把n分段遞迴求解相乘
{
    if (m == 0) return 1;
    return (LL)Cm(n % p, m % p, p) * (LL)Lucas(n / p, m / p, p) % p;
}

int main()
{
    // freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin);
    int  T;
    cin >> T;
    while (T--)
    {
        scanf("%lld%lld%lld", &n, &m, &p);
        printf("%d\n", Lucas(n + m, m, p));
    }
    return 0;
}