传送门

数列的特征方程和特征根老师上课好像讲过然而我没听……以后老师上数学课要认真听了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. 虚拟机linux安装mysql

    安装mysql时需要的全套安装包 mysql-5.1.73-3.el6_5.i686.rpm mysql-libs-5.1.73-3.el6_5.i686.rpm mysql-server-5.1.7 ...

  2. (转)CentOS6.5安装Darwin Streaming Server搭建RTSP流媒体服务器

    参考: 1,CentOS6.5安装Darwin Streaming Server搭建RTSP流媒体服务器 http://www.yimiju.com/articles/567.html

  3. Microsoft.AspNetCore.Identity 使用 mysql 报错处理

    1.使用mysql 首先要确定mysql connector 支的版本,正面是链接 https://dev.mysql.com/doc/connector-net/en/connector-net-e ...

  4. Xmpp学习之Asmack取经-asmack入门(一)

    1.XMPPConnection:它主要是用来创建一个跟XMPP服务端的Socket连接.它是与Jabber服务端的默认连接并且已经在RFC 3920中精确定义过了.示例如下: XMPPConnect ...

  5. jmeter测试总结

    一次性能测试的总结 相关推荐:Apusic应用服务器的性能调节_JVM优化 Apusic应用服务器作为企业应用的运行平台,系统的性能非常重要.当应用对性能的要求比较苛刻时,就要考虑是否需要改变系统的缺 ...

  6. 找到bashrc

    (1)直接sudo gedit ~/.bashrc就可以了,编辑完后关闭就行 (2)主文件夹下ctrl+h就能找到.bashrc文件 之所以要找到bashrc文件,是为了把命令 source /opt ...

  7. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

  8. 关于VLOOKUP函数的用法

    “Lookup”的汉语意思是“查找”,在Excel中与“Lookup”相关的函数有三个:VLOOKUP.HLOOKUO和LOOKUP.下面介绍VLOOKUP函数的用法. 一.功能 在表格的首列查找指定 ...

  9. jmeter中的响应断言

    断言就类似LoadRunner中的检查点.对上一个请求返回的信息,做字符串.数据包大小.HTML.XML.图片等做判断,确保返回的信息的准确性. jmeter的断言有好多,下面是一个响应断言 新建一个 ...

  10. hdu 1209 Clock(排序)

    题意:按钟表的时针.分针的夹角对5个时间进行升序排序,输出第3个时间 思路:排序 注意:若夹角相同,则按时间进行升序排序 #include<iostream> #include<st ...