传送门

数列的特征方程和特征根老师上课好像讲过然而我没听……以后老师上数学课要认真听了QAQ

设\(x=\frac{1+\sqrt{5}}{2},y=\frac{1-\sqrt{5}}{2}\),那么\(x,y\)是\(t^2=t+1\)的两个解,也就是数列\(F_n=F_{n-1}+F_{n-2}\)的特征根

关于特征根是个什么神仙……可以这样理解,假设数列有\(F_n=c_1F_{n-1}+c_2F_{n-2}\),则方程的特征根\(x_1,x_2\)为\(x^2=c_1x+c_2\)的两个解,那么原数列的通项公式为\(F_n=Ax_1^n+Bx_2^n\)

于是在这里我们令\(A=B=1\),那么数列为\(F_n=x^n+y^n\),代入得\(F_1=1,F_2=3\),然后就可以用矩阵快速幂求出\(F_n\),就能知道\(x^n=F_n-y^n\)

当\(n\)为奇数的时候,有\(-1<y^n<0\),则\(\lceil x^n\rceil=F_n+1\)

当\(n\)为偶数的时候,有\(0<y^n<1\),则\(\lceil x^n\rceil=F_n\)

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int P=998244353;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
struct Matrix{
int a[2][2];
Matrix(){a[0][0]=a[0][1]=a[1][0]=a[1][1]=0;}
inline int* operator [](const int &x){return a[x];}
Matrix operator *(Matrix b){
Matrix res;
res[0][0]=add(mul(a[0][0],b[0][0]),mul(a[0][1],b[1][0]));
res[0][1]=add(mul(a[0][0],b[0][1]),mul(a[0][1],b[1][1]));
res[1][0]=add(mul(a[1][0],b[0][0]),mul(a[1][1],b[1][0]));
res[1][1]=add(mul(a[1][0],b[0][1]),mul(a[1][1],b[1][1]));
return res;
}
}A,B;
ll n;
Matrix ksm(Matrix x,ll y){
Matrix res;res[0][0]=res[1][1]=1;
for(;y;y>>=1,x=x*x)if(y&1)res=res*x;
return res;
}
int main(){
// freopen("testdata.in","r",stdin);
A[0][0]=A[0][1]=A[1][0]=1;
int T=read();
while(T--){
n=read(),B[0][0]=3,B[0][1]=1;
if(n<=2){print(n&1?2:3);continue;}
B=B*ksm(A,n-2);
print(B[0][0]+(n&1));
}return Ot(),0;
}

P5136 sequence(矩阵快速幂)的更多相关文章

  1. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  2. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  4. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  5. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  6. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  7. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  8. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  9. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  10. 5950 Recursive sequence (矩阵快速幂)

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

随机推荐

  1. java socket相关的timeout

    1 java socket的两个timeout 一个是connect timeout,即建立连接的timeout,另外一个是so timeout,是读取数据的timeout.这两个timeout都是因 ...

  2. JS中正则匹配开头不带空格,结尾也不带空格的字符串

    在做项目的时候,要求限制SSID的长度.以及开头和结尾不能是空格. var reg = /^\S.{0,30}\S$/ "$$$  $$".match(reg);   ==> ...

  3. Proxy Pattern

    1.Proxy Pattern最大的好处就是实现了逻辑和实现的彻底解耦. 2.Proxy Pattern结构图 3.实现 #ifndef _PROXY_H_ #define _PROXY_H_ cla ...

  4. Struts status

       #status.odd        是否奇数行    #status.count    当前行数    #status.index    当前行的序号,从0开始[#status.count=# ...

  5. WebDriver API——鼠标及键盘操作Actions

    在自动化中我们可能需要用到鼠标或者是键盘操作,在webdriver中是Actions类进行这些操作的. 代码如下: Actions action = new Actions(driver); //-- ...

  6. Nginx配置故障转移

    当上游服务器(真实访问服务器),一旦出现故障或者是没有及时相应的话,应该直接轮训到下一台服务器,保证服务器的高可用. 如果上游服务器的某一台宕机了,直接轮训到下一个~ 8080 8081 8082 关 ...

  7. Android工程的目录结构

    1.最大限度的将不需要出现在Java代码中的文件和代码本身分离开来 2.使用XML标记语言定义UI和数据结构 3.对于工程中的文件存储在工程目录中的那个位置有着严格的规定,在编译过程中Android会 ...

  8. yii表单的各种验证

    /验证规则详细配置 public function rules() { // NOTE: you should only define rules for those attributes that ...

  9. 组合优化学习笔记<之>从贪心算法到子集系统再到拟阵

    贪心算法是用的比较多的一种优化算法,因为它过程简洁优美,而且结果有效.有些优化问题如最大权森林(MWF)是可以用贪心问题求解的,由于最小支撑树(MST)问题与MWF是等价的,所以MST也是可以用贪心算 ...

  10. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...