Skip to content

Replace p with m notation #1013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2023
Merged

Replace p with m notation #1013

merged 3 commits into from
Jan 15, 2023

Conversation

jxu
Copy link
Contributor

@jxu jxu commented Jan 15, 2023

p_i should only be used for primes, while CRT works for any coprime m_i (esp. case of prime powers p^e)

Feel free to come up with a more informative commit msg. I'm tired

p_i should only be used for primes, while CRT works for any coprime m_i (esp. case of prime powers p^e)
@github-actions
Copy link
Contributor

Visit the preview URL for this PR (for commit 8bf848e):

https://cp-algorithms--preview-1013-drvxtt8m.web.app

(expires 2023-01-22T04:14:18.506363056Z)


## Garner's Algorithm

Another consequence of the CRT is that we can represent big numbers using an array of small integers. For example, let $p$ be the product of the first $1000$ primes. From calculations we can see that $p$ has around $3000$ digits.
Another consequence of the CRT is that we can represent big numbers using an array of small integers. For example, let $p$ be the product of the first $1000$ primes. From calculations we can see that $P$ has around $3000$ digits.
Copy link
Contributor Author

@jxu jxu Jan 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use capital P here but avoid touching the rest of the Garner's Algorithm section (which I'm not sure how useful it is) by leaving the usage of p_i

@github-actions
Copy link
Contributor

Visit the preview URL for this PR (for commit 0b18053):

https://cp-algorithms--preview-1013-drvxtt8m.web.app

(expires 2023-01-22T14:18:12.206390573Z)

@jxu
Copy link
Contributor Author

jxu commented Jan 15, 2023

Also is the Java code for Garner's algorithm useful? Wouldn't a bigint implementation in pure C++ be more useful for the vast majority of CPers?

@github-actions
Copy link
Contributor

Visit the preview URL for this PR (for commit 1ecb5fa):

https://cp-algorithms--preview-1013-drvxtt8m.web.app

(expires 2023-01-22T15:23:27.202152721Z)

@adamant-pwn
Copy link
Member

I'm not sure Garner's algorithm is useful at all in competitive programming context... I would also probably prefer Python implementation.

@adamant-pwn adamant-pwn merged commit 29b42e8 into cp-algorithms:master Jan 15, 2023
@jxu
Copy link
Contributor Author

jxu commented Jan 15, 2023

I'm not sure Garner's algorithm is useful at all in competitive programming context... I would also probably prefer Python implementation.

Do we want to get rid of that section or just leave it if it doesn't hurt anything

@jakobkogler
Copy link
Member

@jxu
I think Garner's algorithm is fine. But we probably should replace the Java implementation with something else, e.g. C++ or Python.
In contests I usually was fine doing the computations with long long. It's quite typically that M is not too big, e.g. smaller than 1e9, then you can do all computations with 64 bit integers.

@jakobkogler
Copy link
Member

Although I've never used Garner's algorithm itself (bad time complexity, complicated).
I've always used this code:

struct Congruence
{
    long long a, m, totient_m;
};

class CRT
{
public:
    void add_congruence(long long a, long long m, long long totient_m) {
        congruences.push_back({a, m, totient_m});
    }

    long long get_unique_solution() {
        long long M = 1;
        for (const auto& c : congruences) {
            M *= c.m;
        }

        long long solution = 0;
        for (const auto& c : congruences) {
            auto b = M / c.m;
            solution = (solution + c.a * b % M * power(b, c.totient_m - 1, c.m)) % M;
        }
        return solution;
    }

    std::vector<Congruence> congruences;
};

Years ago I wrote some explanation to it here: https://discuss.codechef.com/t/problem-in-understanding-chineese-remainder-theorem/15899/2?u=afaxnraner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy