Solution to a problem [P1313] to calculate the coefficient

This question is a very bare binomial theorem

The results it is clear that
\ [C ^ {min (n
, m)} _ {k} \ times a ^ {m} \ times b ^ {n} \] First \ (a ^ {m} \ ) and \ (B ^ {n} \) is calculated by flash power much to say

But we \ (C ^ {min (n , m)} _ {k} \) Comparative downright

Compositional Formula look
\ [C ^ {m} _ {n} = \ frac {n!} {M! (Nm)!} = \ Frac {n (n-1) (n-2) \ cdots ( n-m + 1)} {
1 \ times2 \ times3 \ times \ cdots \ times m} \] see this formula it is clear that we can not be directly modulo a divider, multiplying the denominator to become the inverse

But I will not inverse how to do this do it?

First, in high school mathematics is no inverse, we do not need to take the mold. So we go about that in the calculation of the number of points combined. A number on a maximum denominator here is not a modulo 1000. So we can enumerate each of the first number on the denominator, and then on to enumerate a number of points about molecules, about these two numbers \ (GCD \) . Because it is a combination of numbers, so in the end we will be able to be divided into about denominator is \ (1 \) case that is waiting in the multiply any time according to the modulo principle like

This avoids the use of inverse

#include <bits/stdc++.h>
#define LL long long
using namespace std;


const int N = 1e6+5;
const LL mod = 10007;
LL a,b,k,n,m,result = 1,t[N];


inline LL gcd(LL a,LL b) {return !b?a:gcd(b,a%b);}

inline LL ksm(LL a,LL b)
{
    register LL ans = 1;
    while(b)
    {
        if(b & 1) ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans % mod;
}


int main()
{
    cin >> b >> a >> k >> n >> m;
        
    result = (result * ksm(b,n)) % mod;
    result = (result * ksm(a,m)) % mod;
    
    n = min(n,m);
    
    for(register int i = 1,j = k - n + 1;i <= n;i ++,j ++) t[i] = j;
    
    for(register int i = 2;i <= n;i ++)
    {
        register LL x = i,y;
        for(register int j = 1;j <= n;j ++)
        {
            if(x > t[j]) y = gcd(x,t[j]);
            else y = gcd(t[j],x);
            if(y == 1) continue;
            x /= y; t[j] /= y;
            if(x == 1) break;
        }
    }
    
    for(register int i = 1;i <= n;i ++) result = (result * t[i]) % mod;
    
    cout << result << endl;
}

Guess you like

Origin www.cnblogs.com/Mark-X/p/11699330.html