Lucas Theorem & Extended Lucas (Lucas)

Theorem expand Lucas & Lucas

Some data from:

1.https://blog.csdn.net/qq_40679299/article/details/80489761

2.https://www.luogu.com.cn/problemnew/solution/P3807

In this article, permutations C (n, m) represents, in the n-th of m, order does not


Front ~ Cheese:

1) multiplicative inverse

If ax≡1 (mod p), and gcd (a, p) = 1 (a prime and p), called the multiplicative inverse of a modulo p for the element x

2) Fermat's little theorem:

^ a (. 1-p) ≡1 (MOD p) (wherein p and a prime)

3) extended Euclidean

Known integers a, b, Extended Euclidean Algorithm can be obtained by a, while b is the greatest common divisor, to find an integer x, y (one of which is likely to be negative), so that they satisfy the equation ax Bezu + by = gcd (a, b)

Inversion yuan general Fermat's Little Theorem can be used or extended Euclidean achieve.


Lucas Theorem

So we now officially introduced lucas theorem.

Scope

Ask C (n, m)% mod, mod generally is a prime number, n, m, is relatively large, and is clearly greater than if mod n, m, and that there is no need to write Lucas, sufficient light inversing

Such as n take the following values ​​m, p is relatively well suited lucas

40118 16827 2521

18506 3902 883

main idea

We want to calculate C (n, m) in which the mathematical combination of data often is relatively large when the need for a modulo operation.

Since the calculated division containing not directly modulus, otherwise there will be problems like the following:

For example, you are 3 * 5 * 7/5% 4

If you properly calculate the answer is 1, and if you first calculate 15, into a modulo 3, then count 21, as modulo 1, then there is no addition to 5, how to do that?

Obviously, this process ignores the problem that the removal of the front and not k * 4/5 treated, and this part once / 5, it may not divisible by 4

And Lucas Theorem Based on this, the division through the inverse element, be converted to a multiplication , a modulo operation that does not produce the above-described consequences

F / A === ask quantity (mod C)

If there are A * X === 1 (mod C)

Then multiply the two sides at the same time, to give the F * X === for quantity (mod C)

Established conditions

(1) mode equations A * X === presence 1 (mod C) solution of

(2) A | F (F % A == 0)

Such division cleverly turned into a multiplication

Theorem Description

Feng Zhigang "elementary number theory" and proof version:

Luo Valley heavyweights prove


Extended Lucas Theorem


Luo Gu P3807

#include<iostream>
#include<cstring>
using namespace std;
typedef long long int LL;
LL p;
LL min(LL a,LL b)
{
    return a<b?a:b;
}

LL up(LL k,LL num)
{
    LL awsl=1;
    while(k)
    {
        if(k&1)awsl=(awsl*num)%p;
        k/=2;
        num=(num*num)%p;
    }
    return awsl%p;
}
LL getx(LL a)
{
if(a==1)return 1;
    return up(p-2,a);
}
LL c(LL m,LL n)
{
    if(m<n)return 0;
    n=min(n,m-n);
    LL ans=1;
    for(int i=1;i<=n;i++)
    {
        ans=ans*(m+1-i)%p*getx(i)%p;
    }
    return ans;
}
LL Lucas(LL m,LL n)
{
    LL ans=1;
    LL cur=1;
    while(cur<=m||cur<=n)cur*=p;
    while(cur)
    {
        ans*=c(m/cur,n/cur);
        m=m%cur;
        n=n%cur;
        cur/=p;
    }
    return ans%p;
}
void solve()
{
    LL n,m;
    cin>>n>>m>>p;
    cout<<Lucas(m+n,n)<<endl;
}
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        solve();
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/et3-tsy/p/12499280.html