Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Square root trick for modular exponentiation #1465

Open
@jxu

Description

@jxu

Is this documented in the sqrt decomposition page? (BTW should be renamed square root to make it easier to search) I saw it in a Project Euler post but probably saw it elsewhere too.

Compute queries a^b mod m in O(1) time after O(sqrt m) preprocessing. It is only a factor of O(sqrt m) better than binary exponentiation but it's a nice idea of time-space tradeoff.

The idea is b only needs to be calculated mod phi(m), then let s = floor sqrt m. Precompute two arrays: a^0, a^1, ... a^s and a^s, a^2s, ... up to a^m. The idea is sqrt m is the balance point for an exponentiation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp