当前位置: 移动技术网 > IT编程>开发语言>C/C++ > BZOJ5118: Fib数列2(二次剩余)

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

2019年03月27日  | 移动技术网IT编程  | 我要评论

美容店加盟,官场红颜小说,天津星美国际影城

题意

题目链接

题目链接

一种做法是直接用欧拉降幂算出\(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 << '\n';}
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) << '\n';
    }
    return 0;
}
/*
2
2
124124
*/

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网