• BZOJ5118: Fib数列2(二次剩余)


    题意

    题目链接

    题目链接

    一种做法是直接用欧拉降幂算出(2^p pmod{p - 1})然后矩阵快速幂。

    但是今天学习了一下二次剩余,也可以用通项公式+二次剩余做。

    就是我们猜想(5)在这个模数下有二次剩余,拉个板子发现真的有。

    然求出来直接做就行了

    #include<bits/stdc++.h> 
    #define Pair pair<int, int>
    #define MP(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #define int long long 
    #define LL long long 
    #define ull unsigned long long 
    #define Fin(x) {freopen(#x".in","r",stdin);}
    #define Fout(x) {freopen(#x".out","w",stdout);}
    using namespace std;
    const int MAXN = 1e6 + 10, mod = 1125899839733759, INF = 1e9 + 10, inv2 = (mod + 1ll) >> 1ll;
    const double eps = 1e-9;
    template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    template <typename A> inline void debug(A a){cout << a << '
    ';}
    template <typename A> inline LL sqr(A x){return 1ll * x * x;}
    template <typename A> A inv(A x) {return fp(x, mod - 2);}
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    namespace TwoRemain {
    int fmul(int a, int p, int Mod = mod) {
    	int base = 0;
    	while(p) {
    		if(p & 1) base = (base + a) % Mod;
    		a = (a + a) % Mod; p >>= 1;
    	}
    	return base;
    }
    int fp(int a, int p, int Mod = mod) {
    	int base = 1;
    	while(p) {
    		if(p & 1) base = fmul(base, a, Mod);
    		p >>= 1; a = fmul(a, a, Mod);
    	}
    	return base;
    }
    int f(int x) {
    	return fp(x, (mod - 1) >> 1);
    }
    struct MyComplex {
    	int a, b;
    	mutable int cn;
    	MyComplex operator * (const MyComplex &rhs)  {
    		return {
    			add(fmul(a, rhs.a, mod), fmul(cn, fmul(b, rhs.b, mod), mod)),
    			add(fmul(a, rhs.b, mod), fmul(b, rhs.a, mod)),
    			cn
    		};
    	}
    };
    MyComplex fp(MyComplex a, int p) {
    	MyComplex base = {1, 0, a.cn};
    	while(p) {
    		if(p & 1) base = base * a;
    		a = a * a; p >>= 1;
    	}
    	return base;
    }
    int TwoSqrt(int n) {
    	if(f(n) == mod - 1) return -1;
    	if(f(n) ==  0) return  0;
    	int a = -1, val = -1;
    	while(val == -1) {
    		a = rand() << 15 | rand();
    		val = add(mul(a, a), -n);
    		if(f(val) != mod - 1) val = -1;
    	}
    	return fp({a, 1, val}, (mod + 1) / 2).a;
    }
    }
    using namespace TwoRemain;
    signed main() {
    	int rm5 = TwoSqrt(5), inv5 = fp(rm5, mod - 2);
    	int A = fmul(add(1, rm5), inv2),	
    		B = fmul(add(1, -rm5 + mod), inv2);
    	int T = read();
    	while(T--) {
    		int N = read(); int pw2 = fp(2, N, mod - 1);
    		int X = fp(A, pw2), Y = fp(B, pw2);
    		cout << fmul(X - Y + mod, inv5) << '
    ';
    	}
        return 0;
    }
    /*
    2
    2
    124124
    */
    
  • 相关阅读:
    lua类对象
    toLua初始化碰到的问题
    Unity经验之谈-DoTween动画结束匿名委托闭包之坑
    toLua关于委托没有注册的解决方案
    xLua使用require改变路径加载Lua脚本
    unity常用的比较函数
    Shader中颜色混合的算法
    UnityShader中插值平滑曲线
    Shader中的Uniforms(只读标识)
    ShaderLab中Properties语义块支持的属性类型
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10603943.html
Copyright © 2020-2023  润新知