sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

64ビットの剰余演算・合同式・平方根

Math/Modular_Arithmetic.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

本体は法1からUINT64_MAXまでの剰余計算を動的確保なしで行います。符号付きの値はNormalizeModで正規化して渡します。法1の剰余は0です。全APIはconstexprで、定数式の評価予算内ならコンパイル時にも使えます。使用例を参照してください。

API

NormalizeMod

constexpr std::uint64_t NormalizeMod(std::int64_t value, std::uint64_t modulus);

符号付き整数を0以上modulus未満へ正規化します。

制約: modulus>=1。

計算量: O(1)。

ModAdd

constexpr std::uint64_t ModAdd(std::uint64_t a, std::uint64_t b, std::uint64_t modulus);

和の剰余を返します。

制約: modulus>=1。a,bは正規化不要。

計算量: O(1)。

ModSubtract

constexpr std::uint64_t ModSubtract(std::uint64_t a, std::uint64_t b, std::uint64_t modulus);

差の剰余を返します。

制約: modulus>=1。a,bは正規化不要。

計算量: O(1)。

ModMultiply

constexpr std::uint64_t ModMultiply(std::uint64_t a, std::uint64_t b, std::uint64_t modulus);

積の剰余を返します。

制約: modulus>=1。a,bは正規化不要。

計算量: 拡張整数経路O(1)、標準経路O(log(m+1))。

ModPow

constexpr std::uint64_t ModPow(std::uint64_t a, std::uint64_t exponent, std::uint64_t modulus);

非負整数乗の剰余を返します。

制約: modulus>=1、指数は非負。

計算量: 剰余乗算の費用をL(m)としてO(log(e+2)*L(m))時間・O(1)領域。

ModInverse

constexpr std::optional<std::uint64_t> ModInverse(std::uint64_t a, std::uint64_t modulus);

逆元をoptionalで返します。存在しなければnullopt、modulus=1では0です。

制約: modulus>=1。逆元がなければnullopt。

計算量: O(log(m+1)*L(m))時間・O(1)領域。合同式列は統合回数分を加える。

Congruence::operator==

friend constexpr bool operator==(const Congruence&, const Congruence&);

剰余と法が等しいかを返します。

制約: なし。

計算量: O(1)。

SolveLinearCongruence

constexpr std::optional<Congruence> SolveLinearCongruence(std::uint64_t a, std::uint64_t b, std::uint64_t modulus);

a*x=bの合同式の解をoptionalで返します。

制約: modulus>=1。

計算量: O(log(m+1)*L(m))時間・O(1)領域。合同式列は統合回数分を加える。

ChineseRemainder

constexpr std::optional<Congruence> ChineseRemainder(Congruence first, Congruence second);
constexpr std::optional<Congruence> ChineseRemainder(std::span<const Congruence> equations);

合同式を統合した解をoptionalで返します。解がなければnullopt、入力が空列なら{0,1}を返します。

制約: 法は正。解のある中間周期がuint64_tに収まる。

計算量: 統合1回にO(log(m+1)L(m))時間。k式なら合計O(k log(m+1)L(m))の上界、追加領域O(1)。

ModSqrt

constexpr std::optional<std::uint64_t> ModSqrt(std::uint64_t a, std::uint64_t prime);

素数法で平方根の小さい方をoptionalで返します。

制約: primeは素数。素数判定は行わない。

計算量: 非平方剰余をz候補調べる場合O((z+log p)log p)回の剰余乗算。

Congruenceは{remainder,modulus}で、解全体をremainder + k*modulusと表します。一次合同式ではmodulusはm/gcd(a,m)、中国剰余定理では元の法のLCMです。

auto inverse = ModInverse(3, 11); // optionalに4。
auto equation = SolveLinearCongruence(4, 6, 10); // {4,5}: x=4 (mod 5)。
auto joined = ChineseRemainder({2,6}, {8,9}); // {8,18}。
auto root = ModSqrt(10, 13); // optionalに6。

前提と失敗

法0は常にinvalid_argumentです。CRTは先頭から式を統合し、解がないと分かった時点で終了します。後続の未参照入力までは検査しません。解のある途中の統合でLCMがUINT64_MAXを超えると、検査省略時もoverflow_errorです。後の式で解がなくなる場合も、この上限を超える中間周期は保持しません。

ModSqrtは2にも対応しますが、素数判定はしません。合成数を渡した場合の結果や終了は保証しません。SKIP_RUNTIME_CHECKSで素数候補の形の検査を除き、法0・不能判定・容量保護は維持します。全APIは入力不変です。

計算量と演算経路

剰余乗算1回の費用をL(m)とすると、累乗はO(log(e+2)L(m))、逆元・一次合同式・CRTの1回の統合はO(log(m+1)L(m))時間です。CRTでは統合法の最大値をmとして数えます。追加領域はいずれもO(1)です。NormalizeMod・ModAdd・ModSubtractはO(1)です。

GCC/Clangの128ビット整数拡張を使える経路ではL(m)=O(1)、標準の広い積ではL(m)=O(log(m+1))です。拡張型は標準C++の型ではありません。SIRKIT_FORCE_STANDARD_ARITHMETICで標準整数の代替経路を選べます。大きい奇数法の累乗・平方根ではMontgomery表現を再利用します。

平方根は素数の形に応じた直接式、またはTonelli–Shanks法を使います。非平方剰余をz候補調べる場合はO((z+log p)*log p)回の剰余乗算で、小さいzを無条件の最悪上界にはしません。直接式の分岐はO(log p)回です。

VerifyはLibrary Checker Sqrt Mod、累乗・合同式はAtCoderのPower・Throne・Oversleepingを使います。64ビット全域と容量超過は独立ローカル参照で補います。

ソースのAPI注記
法mは1..UINT64_MAX。以下の戻り値は特記しない限り0..m-1。引数a,bは正規化不要。
NormalizeMod(int64_t a,uint64_t m): 負数を含めた標準剰余。O(1)。INT64_MINにも対応する。
ModAdd/ModSubtract/ModMultiply(uint64_t a,b,m): 和/差/積の剰余。符号なし64bit全域で中間オーバーフローを避ける。
ModPow(uint64_t a,exponent,m): a^exponent mod m。0^0は1 mod m。ModMultiplyをO(log(exponent+2))回使う。
以下のL(m)はModMultiply1回の費用。ModInverse(a,m): optional<uint64_t>、逆元なしはnullopt、m=1では0。
ModInverseはO(log(m+1)*L(m))時間。型はuint64_t。入力は正規化不要。
Congruence {uint64_t remainder=0,modulus=1}: x=remainder (mod modulus)。modulus>0が前提。==は両フィールドを比較。
SolveLinearCongruence(uint64_t a,b,m): ax=b (mod m)のoptional<Congruence>。最小非負解と周期、不能ならnullopt。
SolveLinearCongruenceはModInverseと同計算量。CRT2式はM=max(両法)としてO(log(M+1)*L(M))時間。
ChineseRemainder(Congruence a,b): 両合同式を統合。法が互いに素でなくても可。不能ならnullopt。
ChineseRemainder(span<const Congruence>): 順に統合し、空なら{0,1}。時間は各統合費用の合計。
全体のLCMがuint64_tに収まることが前提。全APIの追加領域はO(1)。
CRTで解がある統合段階のLCMがuint64_tを超えると、検査省略時もoverflow_error。不能を検出した後は終了する。
ModSqrt(uint64_t a,prime): x²=a (mod prime)のoptional<uint64_t>。根があれば小さい方、なければnullopt。
primeは素数が前提(素数判定は行わない)。2にも対応。z個の非平方剰余候補を調べる場合、O((z+log p)log p)回の剰余乗算。
ModMultiplyは拡張128bit整数が使えればO(1)、標準代替はO(log m)。ModAdd/Subtract/NormalizeはO(1)。
その他の計算量は上記の剰余乗算1回の費用を含める。全APIは入力不変。正常終了は動的確保なし。
SIRKIT_FORCE_STANDARD_ARITHMETICで128bit拡張を除外。SKIP_RUNTIME_CHECKSで素数候補の形の入力検査を除く。
全APIと内部演算はconstexpr。正常な入力はコンパイラの定数式評価予算内で定数式にも使える。
GCC/Clangの128bit拡張では、奇数法>2^32・指数>=2^15の累乗にMontgomery還元を使う。
専用補助を外へ分け、小さい法のinline経路を保つ。標準代替とconstexprも同じ結果を返す。
ModSqrtはp%4==3・p%8==5を直接式と二乗で判定し、その他はTonelli–Shanks法を使う。
128bit拡張ではp>2^32、またはp>=2^16かつp%8==1のとき、Montgomery表現を保って計算する。
法0は演算を定義できないため、検査省略時もinvalid_argument。
素数性や後続の未参照合同式は検査しない。不能の判定・容量保護は検査省略時も維持する。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
E - Oversleeping chinese_remainder.cpp 未提出 —
E - Throne linear_congruence.cpp 未提出 —
050 - Power modular_power.cpp 未提出 —
Sqrt Mod modular_sqrt.cpp 未提出 —

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 未測定
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(機能とVerify問題): https://judge.yosupo.jp/problem/sqrt_mod
// 独立実装。整数演算・Euclidの互除法・中国剰余定理・Tonelli–Shanks法を用いる。
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/modular_arithmetic.md
// API一覧
// 法mは1..UINT64_MAX。以下の戻り値は特記しない限り0..m-1。引数a,bは正規化不要。
// NormalizeMod(int64_t a,uint64_t m): 負数を含めた標準剰余。O(1)。INT64_MINにも対応する。
// ModAdd/ModSubtract/ModMultiply(uint64_t a,b,m): 和/差/積の剰余。符号なし64bit全域で中間オーバーフローを避ける。
// ModPow(uint64_t a,exponent,m): a^exponent mod m。0^0は1 mod m。ModMultiplyをO(log(exponent+2))回使う。
// 以下のL(m)はModMultiply1回の費用。ModInverse(a,m): optional<uint64_t>、逆元なしはnullopt、m=1では0。
// ModInverseはO(log(m+1)*L(m))時間。型はuint64_t。入力は正規化不要。
// Congruence {uint64_t remainder=0,modulus=1}: x=remainder (mod modulus)。modulus>0が前提。==は両フィールドを比較。
// SolveLinearCongruence(uint64_t a,b,m): ax=b (mod m)のoptional<Congruence>。最小非負解と周期、不能ならnullopt。
// SolveLinearCongruenceはModInverseと同計算量。CRT2式はM=max(両法)としてO(log(M+1)*L(M))時間。
// ChineseRemainder(Congruence a,b): 両合同式を統合。法が互いに素でなくても可。不能ならnullopt。
// ChineseRemainder(span<const Congruence>): 順に統合し、空なら{0,1}。時間は各統合費用の合計。
// 全体のLCMがuint64_tに収まることが前提。全APIの追加領域はO(1)。
// CRTで解がある統合段階のLCMがuint64_tを超えると、検査省略時もoverflow_error。不能を検出した後は終了する。
// ModSqrt(uint64_t a,prime): x²=a (mod prime)のoptional<uint64_t>。根があれば小さい方、なければnullopt。
// primeは素数が前提(素数判定は行わない)。2にも対応。z個の非平方剰余候補を調べる場合、O((z+log p)log p)回の剰余乗算。
// ModMultiplyは拡張128bit整数が使えればO(1)、標準代替はO(log m)。ModAdd/Subtract/NormalizeはO(1)。
// その他の計算量は上記の剰余乗算1回の費用を含める。全APIは入力不変。正常終了は動的確保なし。
// SIRKIT_FORCE_STANDARD_ARITHMETICで128bit拡張を除外。SKIP_RUNTIME_CHECKSで素数候補の形の入力検査を除く。
// 全APIと内部演算はconstexpr。正常な入力はコンパイラの定数式評価予算内で定数式にも使える。
// GCC/Clangの128bit拡張では、奇数法>2^32・指数>=2^15の累乗にMontgomery還元を使う。
// 専用補助を外へ分け、小さい法のinline経路を保つ。標準代替とconstexprも同じ結果を返す。
// ModSqrtはp%4==3・p%8==5を直接式と二乗で判定し、その他はTonelli–Shanks法を使う。
// 128bit拡張ではp>2^32、またはp>=2^16かつp%8==1のとき、Montgomery表現を保って計算する。
// 法0は演算を定義できないため、検査省略時もinvalid_argument。
// 素数性や後続の未参照合同式は検査しない。不能の判定・容量保護は検査省略時も維持する。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <bit>
#include <cstdint>
#include <limits>
#include <numeric>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_MODULAR_ARITHMETIC_DETAIL_DEFINED
#define SIRKIT_MODULAR_ARITHMETIC_DETAIL_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
inline constexpr void check_modulus64(std::uint64_t modulus) {
    if (!modulus) throw std::invalid_argument("modular arithmetic: zero modulus");
}
inline constexpr std::uint64_t modular_add_residues64(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) noexcept {
    return a >= modulus - b ? a - (modulus - b) : a + b;
}
inline constexpr std::uint64_t modular_subtract_residues64(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) noexcept {
    return a >= b ? a - b : modulus - (b - a);
}
inline constexpr std::uint64_t modular_multiply_full64(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) noexcept {
#if defined(__SIZEOF_INT128__) && (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_ARITHMETIC)
    return static_cast<std::uint64_t>(static_cast<unsigned __int128>(a) * b % modulus);
#else
    a %= modulus; b %= modulus;
    if (a < b) std::swap(a, b);
    std::uint64_t result = 0;
    while (b) {
        if (b & 1) result = modular_add_residues64(result, a, modulus);
        b >>= 1;
        if (b) a = modular_add_residues64(a, a, modulus);
    }
    return result;
#endif
}
inline constexpr std::uint64_t modular_multiply64(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) noexcept {
    if ((a | b) <= std::numeric_limits<std::uint32_t>::max()) return a * b % modulus;
    return modular_multiply_full64(a,b,modulus);
}
#if defined(__SIZEOF_INT128__) && (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_ARITHMETIC)
#ifndef SIRKIT_MODULAR_MONTGOMERY64_DEFINED
#define SIRKIT_MODULAR_MONTGOMERY64_DEFINED
// 奇数法・正規化済みの2項。減算REDCの値は(-modulus,modulus)内で、129bitの和を作らない。
class ModularMontgomery64 {
    using Wide=unsigned __int128;
    std::uint64_t modulus_,inverse_,r_squared_;
public:
    explicit constexpr ModularMontgomery64(std::uint64_t modulus) noexcept
        :modulus_(modulus),inverse_(modulus),r_squared_(static_cast<std::uint64_t>((Wide{0}-modulus)%modulus)) {
        // 奇数の二乗はmod8で1。5回のNewton反復で64bit以上の逆元を得る。
        for(unsigned i=0;i<5;++i)inverse_*=2u-modulus_*inverse_;
    }
    constexpr std::uint64_t multiply(std::uint64_t a,std::uint64_t b)const noexcept {
        const Wide product=Wide{a}*b;
        const auto high=static_cast<std::uint64_t>(product>>64);
        const auto correction=static_cast<std::uint64_t>((Wide{static_cast<std::uint64_t>(product)*inverse_}*modulus_)>>64);
        const auto value=high-correction;
        return high<correction?value+modulus_:value;
    }
    constexpr std::uint64_t encode(std::uint64_t residue)const noexcept{return multiply(residue,r_squared_);}
    constexpr std::uint64_t decode(std::uint64_t residue)const noexcept{return multiply(residue,1);}
    template<bool LeftToRight=true>
    constexpr std::uint64_t power(std::uint64_t residue,std::uint64_t exponent)const noexcept {
        if(!exponent)return 1%modulus_;
        auto base=encode(residue);
        if constexpr(LeftToRight){
            auto value=base;
            for(auto bit=std::bit_floor(exponent)>>1;bit;bit>>=1){value=multiply(value,value);if(exponent&bit)value=multiply(value,base);}
            return decode(value);
        }else{
            auto value=encode(1);
            while(exponent){if(exponent&1)value=multiply(value,base);exponent>>=1;if(exponent)base=multiply(base,base);}
            return decode(value);
        }
    }
    // 入出力ともMontgomery表現。sqrt中で表現を保ち、変換を繰り返さない。
    template<bool LeftToRight>
    constexpr std::uint64_t power_encoded(std::uint64_t base,std::uint64_t exponent)const noexcept {
        if(!exponent)return encode(1);
        if constexpr(LeftToRight){
            auto value=base;
            for(auto bit=std::bit_floor(exponent)>>1;bit;bit>>=1){value=multiply(value,value);if(exponent&bit)value=multiply(value,base);}
            return value;
        }else{
            auto value=encode(1);
            while(exponent){if(exponent&1)value=multiply(value,base);exponent>>=1;if(exponent)base=multiply(base,base);}
            return value;
        }
    }
};
[[gnu::noinline]] inline constexpr std::uint64_t modular_montgomery_power64(std::uint64_t a,std::uint64_t exponent,std::uint64_t modulus) noexcept {
    const ModularMontgomery64 arithmetic(modulus);
    if(std::has_single_bit(exponent))return arithmetic.power<true>(a,exponent);
    return arithmetic.power<false>(a,exponent);
}

[[gnu::noinline]] inline constexpr std::optional<std::uint64_t> modular_montgomery_sqrt64(std::uint64_t a,std::uint64_t prime) noexcept {
    const ModularMontgomery64 arithmetic(prime);const auto one=arithmetic.encode(1),encoded_a=arithmetic.encode(a);
    const auto power=[&arithmetic](std::uint64_t base,std::uint64_t exponent)constexpr noexcept {
        if(std::has_single_bit(exponent))return arithmetic.power_encoded<true>(base,exponent);
        return arithmetic.power_encoded<false>(base,exponent);
    };
    if(prime%4==3){
        const auto encoded_root=power(encoded_a,prime/4+1);
        if(arithmetic.multiply(encoded_root,encoded_root)!=encoded_a)return std::nullopt;
        const auto root=arithmetic.decode(encoded_root);return root<prime-root?root:prime-root;
    }
    // p=8k+5: r=a^(k+1)。r²=-aなら2^((p-1)/4)を掛けて補正する。
    if(prime%8==5){
        auto root=power(encoded_a,prime/8+1);const auto square=arithmetic.multiply(root,root);
        if(square!=encoded_a){
            if(square!=prime-encoded_a)return std::nullopt;
            root=arithmetic.multiply(root,power(arithmetic.encode(2),prime/4));
        }
        const auto result=arithmetic.decode(root);return result<prime-result?result:prime-result;
    }
    if(power(encoded_a,(prime-1)/2)!=one)return std::nullopt;
    const auto shifts=static_cast<unsigned>(std::countr_zero(prime-1));const auto odd=(prime-1)>>shifts;
    std::uint64_t non_residue=2;
    while(power(arithmetic.encode(non_residue),(prime-1)/2)!=prime-one)++non_residue;
    auto c=power(arithmetic.encode(non_residue),odd),root=power(encoded_a,(odd+1)/2),t=power(encoded_a,odd);auto remaining=shifts;
    while(t!=one){
        auto value=arithmetic.multiply(t,t);unsigned i=1;
        while(i<remaining&&value!=one){value=arithmetic.multiply(value,value);++i;}
        if(i==remaining)return std::nullopt;
        auto factor=c;for(unsigned j=0;j<remaining-i-1;++j)factor=arithmetic.multiply(factor,factor);
        root=arithmetic.multiply(root,factor);c=arithmetic.multiply(factor,factor);t=arithmetic.multiply(t,c);remaining=i;
    }
    const auto result=arithmetic.decode(root);return result<prime-result?result:prime-result;
}

#endif
#endif

template<bool Narrow>
inline constexpr std::uint64_t modular_power_residue64(std::uint64_t base, std::uint64_t exponent, std::uint64_t modulus) noexcept {
    std::uint64_t result = 1 % modulus;
    while (exponent) {
        if (exponent & 1) {
            if constexpr (Narrow) result = result * base % modulus;
            else result = modular_multiply_full64(result, base, modulus);
        }
        exponent >>= 1;
        if (exponent) {
            if constexpr (Narrow) base = base * base % modulus;
            else base = modular_multiply_full64(base, base, modulus);
        }
    }
    return result;
}
}
#endif

#ifndef SIRKIT_MODULAR_ARITHMETIC_DEFINED
#define SIRKIT_MODULAR_ARITHMETIC_DEFINED
inline constexpr std::uint64_t NormalizeMod(std::int64_t value, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    if (value >= 0) return static_cast<std::uint64_t>(value) % modulus;
    const auto residue = (std::uint64_t{0} - static_cast<std::uint64_t>(value)) % modulus;
    return residue ? modulus - residue : 0;
}
inline constexpr std::uint64_t ModAdd(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_add_residues64(a % modulus, b % modulus, modulus);
}
inline constexpr std::uint64_t ModSubtract(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_subtract_residues64(a % modulus, b % modulus, modulus);
}
inline constexpr std::uint64_t ModMultiply(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(a, b, modulus);
}
inline constexpr std::uint64_t ModPow(std::uint64_t a, std::uint64_t exponent, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    a %= modulus;
    if (!exponent) return 1 % modulus;
    if (a <= 1) return a;
    if (modulus <= (std::uint64_t{1} << 32))
        return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_power_residue64<true>(a, exponent, modulus);
#if defined(__SIZEOF_INT128__) && (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_ARITHMETIC)
    if(exponent>=(std::uint64_t{1}<<15)&&(modulus&1))return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_montgomery_power64(a,exponent,modulus);
#endif
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_power_residue64<false>(a, exponent, modulus);
}
inline constexpr std::optional<std::uint64_t> ModInverse(std::uint64_t a, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    a %= modulus;
    auto r0 = modulus, r1 = a;
#if defined(__SIZEOF_INT128__) && (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_ARITHMETIC)
    __int128 u0 = 0, u1 = 1;
    while (r1) {
        const auto quotient = r0 / r1, remainder = r0 % r1;
        // Euclidの係数は絶対値modulus以下。中間値も符号付き128bitに収まる。
        const auto next = u0 - static_cast<__int128>(quotient) * u1;
        r0 = r1; r1 = remainder; u0 = u1; u1 = next;
    }
    if (r0 != 1) return std::nullopt;
    if (u0 < 0) u0 += modulus;
    return static_cast<std::uint64_t>(u0);
#else
    std::uint64_t u0 = 0, u1 = 1 % modulus;
    while (r1) {
        const auto quotient = r0 / r1, remainder = r0 % r1;
        const auto next = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_subtract_residues64(u0,
            /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(quotient, u1, modulus), modulus);
        r0 = r1; r1 = remainder; u0 = u1; u1 = next;
    }
    if (r0 != 1) return std::nullopt;
    return u0;
#endif
}
struct Congruence {
    std::uint64_t remainder = 0, modulus = 1;
    friend constexpr bool operator==(const Congruence& lhs, const Congruence& rhs) noexcept {
        return lhs.remainder == rhs.remainder && lhs.modulus == rhs.modulus;
    }
};
inline constexpr std::optional<Congruence> SolveLinearCongruence(std::uint64_t a, std::uint64_t b, std::uint64_t modulus) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(modulus);
    a %= modulus; b %= modulus;
    const auto divisor = std::gcd(a, modulus);
    if (b % divisor) return std::nullopt;
    const auto period = modulus / divisor;
    const auto inverse = ModInverse(a / divisor, period);
    return Congruence{/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(b / divisor, *inverse, period), period};
}
inline constexpr std::optional<Congruence> ChineseRemainder(Congruence first, Congruence second) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(first.modulus);
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(second.modulus);
    first.remainder %= first.modulus; second.remainder %= second.modulus;
    const auto divisor = std::gcd(first.modulus, second.modulus);
    if (first.remainder % divisor != second.remainder % divisor) return std::nullopt;
    const auto reduced_second = second.modulus / divisor;
    if (first.modulus > std::numeric_limits<std::uint64_t>::max() / reduced_second)
        throw std::overflow_error("ChineseRemainder: modulus overflow");
    const auto combined_modulus = first.modulus * reduced_second;
    if (reduced_second == 1) return Congruence{first.remainder, combined_modulus};
    std::uint64_t delta;
    if (second.remainder >= first.remainder) delta = (second.remainder - first.remainder) / divisor % reduced_second;
    else {
        const auto residue = (first.remainder - second.remainder) / divisor % reduced_second;
        delta = residue ? reduced_second - residue : 0;
    }
    const auto inverse = ModInverse(first.modulus / divisor, reduced_second);
    const auto multiplier = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(delta, *inverse, reduced_second);
    return Congruence{first.remainder + first.modulus * multiplier, combined_modulus};
}
inline constexpr std::optional<Congruence> ChineseRemainder(std::span<const Congruence> equations) {
    Congruence result;
    for (auto equation : equations) {
        const auto next = ChineseRemainder(result, equation);
        if (!next) return std::nullopt;
        result = *next;
    }
    return result;
}
inline constexpr std::optional<std::uint64_t> ModSqrt(std::uint64_t a, std::uint64_t prime) {
    /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::check_modulus64(prime);
#ifndef SKIP_RUNTIME_CHECKS
    if (prime < 2 || (prime != 2 && !(prime & 1))) throw std::invalid_argument("ModSqrt: prime required");
#endif
    a %= prime;
    if (!a || prime == 2) return a;
    if(a==1)return a;
#if defined(__SIZEOF_INT128__) && (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_ARITHMETIC)
    if((prime>(std::uint64_t{1}<<32)||(prime>=(std::uint64_t{1}<<16)&&prime%8==1))&&(prime&1))return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_montgomery_sqrt64(a,prime);
#endif
    if (prime % 4 == 3) {
        const auto root = ModPow(a, prime / 4 + 1, prime);
        if(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(root,root,prime)!=a)return std::nullopt;
        return root < prime - root ? root : prime - root;
    }
    if(prime%8==5){
        auto root=ModPow(a,prime/8+1,prime);const auto square=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(root,root,prime);
        if(square!=a){
            if(square!=prime-a)return std::nullopt;
            root=/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(root,ModPow(2,prime/4,prime),prime);
        }
        return root<prime-root?root:prime-root;
    }
    if (ModPow(a, (prime - 1) / 2, prime) != 1) return std::nullopt;
    const auto shifts = static_cast<unsigned>(std::countr_zero(prime - 1));
    const auto odd = (prime - 1) >> shifts;
    std::uint64_t non_residue = 2;
    while (ModPow(non_residue, (prime - 1) / 2, prime) != prime - 1) ++non_residue;
    auto c = ModPow(non_residue, odd, prime), root = ModPow(a, (odd + 1) / 2, prime), t = ModPow(a, odd, prime);
    auto remaining = shifts;
    while (t != 1) {
        auto value = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(t, t, prime);
        unsigned i = 1;
        while (i < remaining && value != 1) { value = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(value, value, prime); ++i; }
        if (i == remaining) return std::nullopt;
        auto factor = c;
        for (unsigned j = 0; j < remaining - i - 1; ++j) factor = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(factor, factor, prime);
        root = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(root, factor, prime);
        c = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(factor, factor, prime);
        t = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::modular_multiply64(t, c, prime);
        remaining = i;
    }
    return root < prime - root ? root : prime - root;
}
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page