• 蒙哥马利模板


    #include<bits/stdc++.h>
    using namespace std;
     
    #include <iostream>
     
    using i64 = long long;
    using u64 = unsigned long long;
    using u128 = __uint128_t;
     
    struct Mod64 {
        Mod64() : n_(0) {}
        Mod64(u64 n) : n_(init(n)) {}
        static u64 modulus() {
            return mod;
        }
        static u64 init(u64 w) {
            return reduce(u128(w) * r2);
        }
        static void set_mod(u64 m) {
            mod = m;
            assert(mod & 1);
            inv = m;
            for (int i = 0; i < 5; ++i) inv *= 2 - inv * m;
            r2 = -u128(m) % m;
        }
        static u64 reduce(u128 x) {
            u64 y = u64(x >> 64) - u64((u128(u64(x) * inv) * mod) >> 64);
            return i64(y) < 0 ? y + mod : y;
        }
        Mod64& operator += (Mod64 rhs) {
            n_ += rhs.n_ - mod;
            if (i64(n_) < 0) n_ += mod;
            return *this;
        }
        Mod64 operator + (Mod64 rhs) const {
            return Mod64(*this) += rhs;
        }
        Mod64& operator *= (Mod64 rhs) {
            n_ = reduce(u128(n_) * rhs.n_);
            return *this;
        }
        Mod64 operator * (Mod64 rhs) const {
            return Mod64(*this) *= rhs;
        }
        u64 get() const {
            return reduce(n_);
        }
        static u64 mod, inv, r2;
        u64 n_;
    };
    u64 Mod64::mod, Mod64::inv, Mod64::r2;
     
    inline u64 mod128_64_small(u128 a, u64 b) {
        u64 q, r;
        __asm__ (
            "divq	%4"
            : "=a"(q), "=d"(r)
            : "0"(u64(a)), "1"(u64(a >> 64)), "rm"(b)
        );
        return r;
    }
     
    Mod64 a[1000005];
     
    int main() {
    #ifdef CX_TEST
        freopen("E:\program--GG\test_in.txt", "r", stdin);
    #endif
        int T;
        cin >> T;
        for(; T--;) {
            i64 m0, m1, a0, a1, c, k, mod;
            scanf("%lld%lld%lld%lld%lld%lld%lld", &a0, &a1, &m0, &m1, &c, &mod, &k);
            Mod64::set_mod(mod);
            Mod64 ret, M0 = Mod64(m0), M1 = Mod64(m1), C = Mod64(c);
            a[0] = Mod64(a0);
            a[1] = Mod64(a1);
            ret = a[0] * a[1];
            for(int i = 2; i <= k; i++) {
                a[i] = M0 * a[i - 1] + M1 * a[i - 2] + C;
                ret *= a[i];
            }
            printf("%llu
    ", ret.get());
        }
    }
    View Code
  • 相关阅读:
    底部菜单栏之Fragment的详细介绍和使用方法
    Warm up 2
    如何做好一位资深的web前端工程师
    使用 HTML5 canvas 绘制精美的图形
    计算元素距离浏览器左边的距离
    [JSOI2016]独特的树叶
    【SDOI2009】Elaxia的路线
    【SCOI2009】最长距离
    【SCOI2009】围豆豆
    【AHOI2005】穿越磁场
  • 原文地址:https://www.cnblogs.com/Aragaki/p/9745280.html
Copyright © 2020-2023  润新知