Skip to content

Optimize and make Modint safer#318

Merged
simonlindholm merged 1 commit into
kth-competitive-programming:mainfrom
Matistjati:safer-mod
Jun 23, 2026
Merged

Optimize and make Modint safer#318
simonlindholm merged 1 commit into
kth-competitive-programming:mainfrom
Matistjati:safer-mod

Conversation

@Matistjati

Copy link
Copy Markdown
Contributor

The old modint was slightly footgunny because it didn't normalize in the constructor, so you could get un-normalized Mod structs if you passed numbers outside of $[0,mod)$. It's quite easy to miss this, especially if the input may contain negative numbers. To avoid performance regression from modding in the constructor, I add an "internal" constructor. The code is noticably faster for + and -. * should be about the same. The number of non-whitespace characters increases by 5.

@Matistjati Matistjati force-pushed the safer-mod branch 2 times, most recently from 832903c to 53f5a20 Compare June 23, 2026 13:57
assert(g == 1); return x;
}
Mod operator^(ll e) {
if (!e) return Mod(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (!e) return Mod(1);
if (!e) return 1;

also works, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup, pushed

@simonlindholm simonlindholm merged commit 96ad8ab into kth-competitive-programming:main Jun 23, 2026
1 check passed
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.

2 participants