CF450B Jzzhu and Sequences

大佬留言:这。这。不就是矩乘的模板吗,切掉它!!

You are given xx and yy , please calculate $f_{n}(mod(10^{9}+7))$.

原式:$f_{i}=f_{i-1}+f_{i+1}$

转换一下:$f_{i+1}=f_{i}-f_{i-1}$

相当于$f_{i}=f_{i-1}-f_{i-2}$

有没有发现它跟斐波那契通项公式有点儿类似?

的确是这样的,那么转移矩阵也类似:

$0 -1$

$1 1$

矩阵快速幂好了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring> #define LL long long
#define N 10
using namespace std; class Martix{
public:
LL n,m;
LL A[N][N];
Martix(){
memset(A,,sizeof(A));
}
};
const LL mod=; Martix operator * (Martix A,Martix B){
Martix C;
int n=A.n,m=B.m,p=A.m;
C.n=n,C.m=m;
for(LL i=;i<=n;i++)
for(LL j=;j<=m;j++)
for(LL k=;k<=p;k++)
C.A[i][j]=((A.A[i][k]*B.A[k][j]+mod)%mod+C.A[i][j]%mod+mod)%mod;
return C;
} LL x,y,n; Martix A,B;
inline LL pow(){
for(;n;n>>=,A=A*A)
if(n&) B=B*A;
return B.A[][];
} int main()
{
scanf("%lld%lld%lld",&x,&y,&n); A.n=A.m=;
A.A[][]=,A.A[][]=-,A.A[][]=,A.A[][]=;
B.n=,B.m=;
B.A[][]=(x+mod)%mod,B.A[][]=(y+mod)%mod;
if(n==) printf("%lld",(x+mod)%mod);
else if(n==) printf("%lld",(y+mod)%mod);
else n-=,printf("%lld\n",pow());
return ;
}

CF450B Jzzhu and Sequences(矩阵加速)的更多相关文章

  1. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  3. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  4. CF450B Jzzhu and Sequences 题解

    Content 有一个长度为 \(n\) 的数列 \(\{a_1,a_2,\dots,a_n\}\),满足如下的递推公式: \(i=1\) 时,\(a_1=x\). \(i=2\) 时,\(a_2=y ...

  5. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  6. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  7. Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

  8. Codeforces450 B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. 数学 找规律 Jzzhu and Sequences

    A - Jzzhu and Sequences   Jzzhu has invented a kind of sequences, they meet the following property: ...

随机推荐

  1. ios29--多线程

    进程是指在系统中正在运行的一个应用程序(一个程序可以对应多个进程).每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内.比如同时打开迅雷.Xcode,系统就会分别启动2个进程.1个进程要 ...

  2. update-java-alternatives 更改默认Java环境

    Ubuntu/debian 更改默认Java环境 我的电脑里安装了两个版本的Java,一个是java-6-sun,还有一个是java-gcjgcj是在JVM非常缓慢的时候诞生的,他可以把Java代码编 ...

  3. Keepalived+lvs 搭建高可用负载均衡

    本站点停止更新,请访问:blog.coocap.com 不了解负载均衡高可用的童鞋,强烈建议先看keepalived+nginx高可用负载均衡: 传送门(求粉):http://www.cnblogs. ...

  4. Linux-----Kconfig文件的简介

    内核源码树的目录下都有两个文件Kconfig和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库, 每个Kconfig分别描述了所属目录源文件相关的内核配置菜单.在内核配置 ...

  5. FreeMarker:

    ylbtech-FreeMarker: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbt ...

  6. easyui图标对照

    转自:https://blog.csdn.net/qq_34545192/article/details/78250816 原作者文章地址: http://www.cnblogs.com/timeme ...

  7. 使用Redis存储Nginx+Tomcat负载均衡集群的Session

    配置Tomcat的session共享可以有三种解决方案: 第一种是以负载均衡服务器本身提供的session共享策略,每种服务期的配置是不一样的并且nginx本身是没有的. 第二种是利用web容器本身的 ...

  8. 大数高精度加减乘除 51nod 1005 大数加法

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...

  9. ACM_递推题目系列之三放苹果(递推dp)

    递推题目系列之三放苹果 Time Limit: 2000/1000ms (Java/Others) Problem Description: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放 ...

  10. PostgreSQL与MySQL比较

    特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例.一个实例可以管理一个或多个数据库.一台服务器可以运行多个 mysqld 实例.一个实例管理器可以监视 ...