中位蓝(?)。
令 为斐波那契数列第 项。
初步判断这题要用矩阵加速以及欧拉降幂。
但是你知道斐波那契数列 有周期性吗?
是的!(具体参照 SP12007 FRS2 - Fibonaccibonacci (easy))
皮萨诺周期
当斐波那契数列 时,皮萨诺周期 为最小正周期。
特别地,当 时,。
到这里这题已经够用了。
思路
本题 。
-
求 。这个直接欧拉降幂。
-
接着 。矩阵加速即可。
似乎会爆 long long,还要补前导 。
复杂度算在矩阵加速上。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define re register
#define endl '\n'
#define ll __int128
#define ull unsigned long long
#define db double
#define fi const int&
#define fl const ll&
#define ful const ull&
#define fc const char&
#define fs const string&
#define debug() puts("I Love Hina Forever")
using namespace std ;
namespace IO {
template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}
template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}
template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}
template<class T>inline void write(T x,fc c){write(x),putchar(c);}
}using namespace IO ;
const int N = 5 ;
const ll p = 1.5e10,m = 1e10 ;//pi(m)以及模数m
ll T,n,t[N] = {0,7,7,7},tot = 4,a ;
struct matrix {
int n,m ;
ll a[N][N] ;
inline matrix () {memset(a,0,sizeof a) ;}
inline void prepare (fi _n,fi _m) {n = _n,m = _m ;}
};
inline matrix operator * (matrix A,matrix B) {
matrix C ;
C.prepare(A.n,B.m) ;
for (re int i = 1 ; i <= C.n ; ++i) {
for (re int j = 1 ; j <= C.m ; ++j) {
for (re int k = 1 ; k <= A.m ; ++k) (C.a[i][j] += A.a[i][k] * B.a[k][j] % m) %= m ;
}
}
return C ;
}
inline matrix operator ^ (matrix A,ll k) {
matrix ans ;
ans.prepare(A.n,A.m) ;
for (re int i = 1 ; i <= ans.n ; ++i) ans.a[i][i] = 1 ;
while (k) {
if (k & 1) ans = ans * A ;
k >>= 1,A = A * A ;
}
return ans ;
}
inline ll f (fl x) {//算F_i mod m
if (x == 0) return 0 ;
if (x == 1) return 1 ;
matrix A,ans ;
ans.prepare(2,1) ;
A.prepare(2,2) ;
ans.a[1][1] = 1,ans.a[2][1] = 1 ;
A.a[1][1] = 0,A.a[1][2] = 1 ;
A.a[2][1] = 1,A.a[2][2] = 1 ;
ans = (A ^ (x - 2)) * ans ;
return ans.a[2][1] ;
}
inline ll phi (ll x) {//算欧拉函数
re ll ans = x ;
for (re ll i = 2 ; i <= x / i ; ++i) {
if (x % i == 0) {
ans = ans / i * (i - 1) ;
while (x % i == 0) x /= i ;
}
}
if (x > 1) ans = ans / x * (x - 1) ;
return ans ;
}
inline ll qpow (ll a,ll b,ll m) {//算a^b mod m,注意扩展欧拉定理的实现(ans与m的大小)
re ll ans = 1,flag = 0 ;
if (a >= m) a %= m,flag = 1 ;
while (b) {
if (b & 1) {
ans = ans * a ;
if (ans >= m) flag = 1,ans %= m ;
}
b >>= 1 ;
if (b) {
a = a * a ;
if (a >= m) flag = 1,a %= m ;
}
}
return flag ? ans + m : ans ;
}
inline ll solve (int pos,ll mod) {
if (mod == 1) return 1 ;//这里要加上phi(m)的,要返回1
if (pos == tot + 1) return 1 ;
return qpow(t[pos],solve(pos + 1,phi(mod)),mod) ;
}
inline void wte (ll x) {//输出,需要就补前导0
vector<int> v ;
while (x) v.push_back(x % 10),x /= 10 ;
for (re int i = 1 ; i <= 10 - v.size() ; ++i) putchar('0') ;
for (re int i = v.size() - 1 ; i >= 0 ; --i) write(v[i]) ;
putchar(endl) ;
}
int main () {
read(T) ;
while (T--) {
read(n),t[4] = n ;
a = solve(1,p) % p ;
a = f(a) ;
wte(a) ;
}
return 0 ;
}