This topic can be translated first:
Make f (1) = 5+2√6
F (2) = f (1) * (5+2√6)
...
F (n) = f (n-1) * (5+2√6)
F (n) = f (n-1) * (10-(5-2√6)) = 10*f (n-1)-(5-2√6) f (n-1) = 10*f (n-1)-10/(5+2√6) f (n-1) = 10*f (n-1)-10/(5+2√6) * (5+2√6) F (n -2)
= 10*f (n-1)-F (n-2)
Then it can be written in the form of matrix multiplication.
(f (n), F (n-1)) = (f (n-1), F (n-2)) (10, 1
-1, 0)
But the 2^x+1 here is still very large, and here we use the generalized Fibonacci sequence to find the idea of a cyclic festival.
Cycle section length = (mod-1) * (mod+1)
The specific proof can be referenced here: Generalized Fibonacci sequence
Then just ask for a power operation on the length of the die cycle.
But here F (i) is a decimal with a square root, and here we choose to replace it with an approximate integer.
5+2√6 = 9.89 ...
F (0) = (5+2√6) ^0 = 1
F (1) = (5+2√6) ^1 = 5+2√6
/* Embarrassed for a long while I still do not know why F (0) replaced by 2, F (1) with 10 instead of a guarantee that the last to be taken is the top * *
1#include <bits/stdc++.h>2 using namespacestd;3 #defineN 1000104 #definell Long Long5 intn,q;6 ll MOD;7 structmatrix{8 intm[2][2];9 voidInit () {m[0][0]=m[1][1]=1; m[0][1]=m[1][0]=0;}TenMatrixoperator*(ConstMatrix &p)Const{ One Matrix ret; A for(intI=0; i<2; i++) - for(intj=0; j<2; J + +){ -ret.m[i][j]=0; the for(intk=0; k<2; k++){ -RET.M[I][J] = (ret.m[i][j]+ ((LL) m[i][k]*p.m[k][j])%mod)%MOD; - } - } + returnret; - } + }; A at intQpow (intb) - { -ll ret=1, a=2; - while(b) { - if(b&1) ret = ret*a%MOD; -A = a*a%MOD; inb>>=1; - } to returnret; + } - theMatrix Qpow (Matrix A,intb) * { $ Matrix ret;Panax Notoginseng ret.init (); - while(b) { the if(b&1) ret = ret*A; +A = A *A; Ab>>=1; the } + returnret; - } $ $ intMain () - { - //freopen ("a.in", "R", stdin); the intT, cas=0; -scanf"%d", &T);Wuyi while(t--) the { -scanf"%d%d", &n, &q); WuMOD = (q1) * (q+1); -n =Qpow (n); AboutMOD =Q; $ Matrix A; -a.m[0][0]=Ten, a.m[1][0]=-1, a.m[0][1]=1, a.m[1][1]=0; -A =Qpow (A, n); -ll val = (ll)Ten*a.m[0][0]+ (LL)2*a.m[1][0]; Aval = ((val%mod) +mod)%MOD; +printf"Case #%d:%i64d\n", ++cas, (val+mod-1)%MOD); the } - return 0; $}
HDU 5451 Generalized Fibonacci sequence