Saturday, July 9, 2022
HomeWordPress DevelopmentCompute nCr % p | Set 4 (Chinese language The rest theorem...

Compute nCr % p | Set 4 (Chinese language The rest theorem with Lucas Theorem)


  

#embrace <bits/stdc++.h>

utilizing namespace std;

#outline ll lengthy lengthy

  

class Resolution {

public:

    

    int nCrModM(int n, int r, int p)

    {

        

        vector<int> primes = findPrimeFactors(p);

        vector<int> rem;

  

        

        for (auto m : primes)

            rem.push_back(Lucas(n, r, m));

  

        

        

        int min_x = 0;

        whereas (true) {

            bool discovered = true;

            for (int i = 0; i < primes.measurement();

                 i++) {

                if (min_x % primes[i] != rem[i]) {

                    discovered = false;

                    break;

                }

            }

            if (discovered) {

                return min_x;

            }

            min_x++;

        }

        

        return min_x;

    }

  

    

    int Lucas(int n, int r, int m)

    {

        

        if (r == 0)

            return 1;

  

        int ni = n % m;

        int ri = r % m;

        return (pascal(ni, ri, m)

                * Lucas(n / m, r / m, m))

               % m;

    }

  

    

    ll pascal(int n, int r, int m)

    {

        if (r == 0 or r == n)

            return 1;

  

        

        int nCr[r + 1];

        memset(nCr, 0, sizeof(nCr));

        nCr[0] = 1;

        for (int i = 1; i <= n; i++) {

            for (int j = min(r, i); j > 0; j--)

                nCr[j]

                    = (nCr[j] + nCr[j - 1]) % m;

        }

        return nCr[r];

    }

  

    

    

    vector<int> findPrimeFactors(int n)

    {

        vector<int> primes;

        if (n % 2 == 0) {

            primes.push_back(2);

            whereas (n % 2 == 0)

                n >>= 1;

        }

        for (int i = 3; n > 1; i += 2) {

            if (n % i == 0) {

                primes.push_back(i);

                whereas (n % i == 0)

                    n /= i;

            }

        }

  

        

        

        return primes;

    }

};

  

int most important()

{

    int n = 10, r = 2, p = 13;

    Resolution obj;

  

    

    int ans = obj.nCrModM(n, r, p);

    cout << ans;

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments