Lucas 定理

Lucas 定理

概述:

Lucas(卢斯卡)定理是用来求C(n,m) mod p的值。其中:n和m是非负整数,p是素数。一般用于m,n很大而p很小,或者n,m不大但大于怕、,这样用阶乘就解决不了问题。

结论:

1.    Lucas(n,m,p)=cm(n\%p,m\%p)*Lucas(n/p,m/p,p)\%p

其中Lucas(x,0,p)==0;    cm(a,b)=a!*(b!*(a-b)!)^{p-2}mod \ p

                           使用逆元的方法,化简=\frac{a!}{(a-b)!)}*(b!)^{p-2}mod \ p   (a-b)!关于p的逆元为(((a-b)!)^{p-2}.

2. 把n写成p进制的数a[n]a[n-1]a[n-2]......a[1]a[0],把b写成p进制的数b[n]b[n-1]b[n-2]......b[1]b[0],则C(n,m)与C(a[n],b[n])*C(a[n-1],b[n-1])*.....C(a[0],b[0])关于模同余。

例题 HDU3037 This is the link

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.

题目大意是有n个盒子,在每个盒子里面放一些球(可以不放),使得总球数小于等于m,求方案数(mod p)

数据:1<=n,m<=1000000000,1<p<100000,p保证数素数

输入样例在上面

思路理解:设有n+1个盒子,将m个球放进n+1个盒子里的方法有多少种,要求球一定在盒子里,就单个盒子来说,可以是空的,我们增加的那个盒子里放的就相当于一开始在外面,没有放的,所以方案有C(n+m,m)种,然后编程实现

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define mod 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了
LL quick_pow(LL a,LL n,LL q)
{
    LL ret=1;
    a%=q;
    while(n)
    {
        if(n&1)
            ret=ret*a%q;
        a=a*a%q;
        n>>=1;
    }
    return ret;
}
LL getc(LL n,LL m,LL q)
{
    if(n<m)
        return 0;
    if(m>n-m)
        m=n-m;
    LL s1=1,s2=1;
    for(int i=0;i<m;++i)
    {
        s1=s1*(n-i)%q;
        s2=s2*(i+1)%q;
    }
    return s1*quick_pow(s2,q-2,q)%q;
}
LL lucas(LL n,LL m,LL q)
{
    if(!m)
        return 1;
    return getc(n%q,m%q,q)*lucas(n/q,m/q,q)%q;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n,m,q;
        scanf("%lld%lld%lld",&n,&m,&q);
        printf("%lld\n",lucas(n+m,m,q));
    }
    return 0;
}

卢斯卡定理的另一种实现

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define mod 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了

LL n,m,p;

LL fact[100100];

LL QuickPow(LL x,LL t,LL m)//快速幂求模
{
	if(t==0) return 1LL;
	LL e=x,ret=1LL;
	while(t)
	{
		if(t&1) ret=(ret*e)%m;
		e=(e*e)%m;
		t>>=1LL;
	}
	return ret%m;
}

void get_fact(LL p)//求阶乘并求模
{
	fact[0]=1LL;
	for(int i=1;i<=p+10;i++)
		fact[i]=(fact[i-1]*i)%p;
}

LL Lucas(LL n,LL m,LL p)//卢斯卡定理的另一种形式,while循环,与递归形式一样
{
	//lucas(n,m,p)=c[n%p][m%p]*lucas(n/p,m/p,p);
	LL ret=1;
	while(n&&m)
	{
		LL a=n%p,b=m%p;
		if(a<b) return 0;
		ret=(ret*fact[a]*QuickPow((fact[b]*fact[a-b])%p,p-2,p))%p;
		n/=p; m/=p;
	}
	return ret%p;
}

int main()
{
	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		LL n,m,p;
		cin>>n>>m>>p;
		get_fact(p);
		cout<<Lucas(n+m,m,p)<<endl;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值