題意 "題目鏈接" 題目鏈接 一種做法是直接用歐拉降冪算出$2^p \pmod{p 1}$然後矩陣快速冪。 但是今天學習了一下二次剩餘,也可以用通項公式+二次剩餘做。 就是我們猜想$5$在這個模數下有二次剩餘,拉個板子發現真的有。 然求出來直接做就行了 cpp include define Pair ...
題意
題目鏈接
一種做法是直接用歐拉降冪算出\(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
*/