P5136 sequence(矩阵快速幂)
数列的特征方程和特征根老师上课好像讲过然而我没听……以后老师上数学课要认真听了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(矩阵快速幂)的更多相关文章
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- 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 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- 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 ...
- 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 ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
随机推荐
- non-blocking I/O
https://en.wikipedia.org/wiki/New_I/O_(Java) zh.wikipedia.org/wiki/Java_NIO Java NIO API提供在java.nio套 ...
- myeclipse10.0如何集成Git
现需要给myeclipse10.0集成git工具,经过搜索资料,现整理如下方法: myeclipse10.0对应的Git版本应该为:egit版本为2.3.1 下载地址:http://wiki.ecli ...
- iOS应用的入口自定义和事件处理的自定义
iOS应用的入口自定义和事件处理的自定义 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: ...
- HDU4686 Arc of Dream —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4686 Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memo ...
- .NET CORE2.0后台管理系统(一)配置API
一:引用关系图 要写一个项目首先离不开的就是一个清晰的流程图,当然我这里很简单. 上诉完成后打开api下的Startup.cs文件,因为我是配置好了所在我直接上传代码然后介绍一下: using Sys ...
- 关于 josnp ,解决跨域问题
JSONP是服务器与客户端跨源通信的常用方法.最大特点就是简单适用,老式浏览器全部支持,服务器不用做任何改造[使用jsonp的时候jsonp: "callback",callbac ...
- 数据表示Numpy
1 基本 1.1 基本介绍 掌握表示, 清洗, 统计和展示数据的能力 Numpy, Matplotlib, Pandas, Projects 摘要: 有损的提取数据特征的过程 可以将一组数据, 摘要出 ...
- 「LOJ#103」子串查找 (Hash
题目描述 这是一道模板题. 给定一个字符串 A A A 和一个字符串 B B B,求 B B B 在 A A A 中的出现次数.AAA 和 BBB 中的字符均为英语大写字母或小写字母. A A A 中 ...
- bzoj 2655 calc —— 拉格朗日插值
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2655 先设 f[i][j] 表示长度为 i 的序列,范围是 1~j 的答案: 则 f[i][ ...
- 【旧文章搬运】关于windbg搜索符号文件的一点说明
原文发表于百度空间,2010-09-07========================================================================== 本来只是打 ...