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数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
随机推荐
- EasyDarwin开源流媒体服务器进行RTSP转发过程中将sdp由文件存储改成内存索引
-本篇由团队成员Fantasy供稿! 原始版本 在Darwin Streaming Server版本中,推送端DoAnnounce的时候后服务器会根据easydarwin.xml中配置的movies_ ...
- 负载均衡实现,一个域名对应多个IP地址
负载均衡实现,一个域名对应多个IP地址 - 宏宇 - 博客园 https://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.h ...
- yum 工作原理
MySQL :: A Quick Guide to Using the MySQL Yum Repository https://dev.mysql.com/doc/mysql-yum-repo-qu ...
- Memory usage of a Java process java Xms Xmx Xmn
http://www.oracle.com/technetwork/java/javase/memleaks-137499.html 3.1 Meaning of OutOfMemoryError O ...
- UVA-11892(组合游戏)
题意: 给n堆石子,每堆有ai个,每次可以取每堆中任意数目的石子;但是上一次操作的人没有将一堆全部取走,那么下一个人还要在那一堆取; 思路: 每次取到这堆就剩一个的策略; AC代码: #include ...
- 数据摘要pandas
主要是用于分析数据的Pandas库 先学习两个数据类型DataFrame和series 进一步学习利用Pandas进行摘要的方法, 提取数据的特征 1 pandas库 1.1 pandas库 pand ...
- [C++]变量存储类别,指针和引用,类与对象,继承与派生的一些摘要
C++中共有四种存储类别标识符:auto/static/register/extern 1.auto 函数或分程序内定义的变量(包括形参)可以定义为auto(自动变量).如果不指定存储类别,则隐式定义 ...
- BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset
BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i ...
- HDU2602(01背包)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1394 树状数组+离散化求逆序数
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...