您的位置:首页 > 其它

【HDU 3037】大数组合取模之Lucas定理+扩展欧几里得求逆元与不定方程一类问题

2014-07-26 22:28 435 查看


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

2009 Multi-University Training
Contest 13 - Host by HIT

转自:http://www.cnblogs.com/kane0526/archive/2012/12/16/2820789.html

解题思路:

题目可以转换成  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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: