From d4541a3f800c476d51f649d530b20610d2bced83 Mon Sep 17 00:00:00 2001 From: Enriquefft Date: Sat, 13 Jul 2024 23:11:25 -0500 Subject: [PATCH] added modulo exponentiation --- src/ModuloExponentiation.cpp | 32 ++++++++++++++++++++++ src/main.cpp | 53 ------------------------------------ 2 files changed, 32 insertions(+), 53 deletions(-) create mode 100644 src/ModuloExponentiation.cpp delete mode 100644 src/main.cpp diff --git a/src/ModuloExponentiation.cpp b/src/ModuloExponentiation.cpp new file mode 100644 index 0000000..d736d9e --- /dev/null +++ b/src/ModuloExponentiation.cpp @@ -0,0 +1,32 @@ +#include +using ll = intmax_t; +using ull = uintmax_t; + +constexpr ll MODULO = 1000000007; + +// exponent is very large +// modulo is prime (1e9 + 7) +// base is always 2 + +/// @brief Binary Exponentiation with modulo +/// @param expo exponent +/// @param base base (default: 2) +/// @param modulo modulo +template +constexpr auto bin_mod_exp(ll expo, ll base = 2, ll modulo = MODULO) -> ll { + + if constexpr (USE_FERMAT_LITTLE_THEOREM) { + expo %= (modulo - 1); + } + + ll res = 1; + + while (expo > 0) { + if (expo & 1LL) { + res = (res * base) % modulo; + } + base = (base * base) % modulo; + expo >>= 1LL; + } + return res; +} diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 460c2b6..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -#if defined(_WIN32) -#define NOGDI // All GDI defines and routines -#define NOUSER // All USER defines and routines -#endif - -#include - -#if defined(_WIN32) // raylib uses these names as function parameters -#undef near -#undef far -#endif - -constexpr int TARGET_FPS = 60; - -auto main() -> int { - - constexpr int SCREEN_WIDTH = 800; - constexpr int SCREEN_HEIGHT = 450; - - constexpr int TEXT_X_POS = 190; - constexpr int TEXT_Y_POS = 200; - constexpr int TEXT_FONT_SIZE = 20; - - raylib::Window window(SCREEN_WIDTH, SCREEN_HEIGHT, - "raylib-cpp - basic window"); - raylib::Texture logo(ASSETS_PATH "dogo.png"); - - SetTargetFPS(TARGET_FPS); - - spdlog::info("Started drawing"); - - while (!window.ShouldClose()) { - BeginDrawing(); - - window.ClearBackground(RAYWHITE); - - // Object methods. - logo.Draw(SCREEN_WIDTH / 2 - logo.GetWidth() / 2, - SCREEN_HEIGHT / 2 - logo.GetHeight() / 2); - - DrawText("Congrats! You created your first window!", TEXT_X_POS, TEXT_Y_POS, - TEXT_FONT_SIZE, LIGHTGRAY); - - EndDrawing(); - } - spdlog::info("Finished drawing"); - - return 0; -}