Skip to content
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

ModInt 2^31 <= m < 2^32 for addition/subtruction #164

Open
mizar opened this issue Apr 18, 2023 · 1 comment
Open

ModInt 2^31 <= m < 2^32 for addition/subtruction #164

mizar opened this issue Apr 18, 2023 · 1 comment

Comments

@mizar
Copy link

mizar commented Apr 18, 2023

The implementation of addition and subtraction in the ModInt structure does not seem to assume $2^{31}\le m\lt 2^{32}$.

Suggested mitigation

  • Addition (plan A, cast 32/64-bit integer):
    • Cast this._v and rhs._v to 64-bit integer type.
    • Performs addition and compares it with the value of umod().
    • If the sum is greater than umod(), subtract the value of umod() from the sum.
    • Cast the result to a 32-bit integer type.
  • Addition (plan B, 32-bit integer overflow detection):
  • Subtraction:
    • If rhs._v is greater than self._v, then self._v - rhs._v + umod(), otherwise self._v - rhs._v.

Code

mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}

mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}

rust-lang-ja/ac-library-rs@ab2c3ed#diff-9c0db0574dc35afe73adabeaba95ec11f15af83bb2cf1d5d70b916b6da84e2eaR793-R812

Related Issue/PR

@yosupo06
Copy link
Collaborator

Thanks for suggestion, but we don't have a plan to extend mod range to m < 2^32. As far as I know, at least 99% usage of modint is m < 2^31. TBH I don't came up any recent problems that requires 2^31 < m < 2^32. So, we prefer to prepare m < 2^31 modint rather than m < 2^32 modint with tricky technique.

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

No branches or pull requests

2 participants