CF450B Jzzhu and Sequences(矩阵加速)
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(矩阵加速)的更多相关文章
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- CF450B Jzzhu and Sequences 题解
Content 有一个长度为 \(n\) 的数列 \(\{a_1,a_2,\dots,a_n\}\),满足如下的递推公式: \(i=1\) 时,\(a_1=x\). \(i=2\) 时,\(a_2=y ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- 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 ...
- Codeforces450 B. Jzzhu and Sequences
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 数学 找规律 Jzzhu and Sequences
A - Jzzhu and Sequences Jzzhu has invented a kind of sequences, they meet the following property: ...
随机推荐
- 【bzoj1029】[JSOI2007]建筑抢修
按照t2从小到大排列之后贪心. 若当前任务可以插入,则插入. 若当前任务不可以插入,分两种情况: ①当前任务的耗时大于等于之前插入的任务的最大耗时:跳过当前任务 ②当前任务的耗时小于之前插入的任务的耗 ...
- JavaScript中的string interpolation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals var a = 5; var b ...
- HDU 1517:A Multiplication Game
A Multiplication Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 12. Ext.Ajax 对ajax的支持
转自:http://www.cnblogs.com/lipan/archive/2011/12/09/2272793.html 本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持,包括Ajax封 ...
- 昆石VOS3000_2.1.2.0完整安装包及安装脚本
安装包下载地址 http://www.51voip.org/post/57.html 安装教程: 上传安装包 ·给整个目录授权 chmod 777 /root/vosintsall 1.安装前准备 首 ...
- java笔记线程方式1获取对象名称
* 如何获取线程对象的名称呢? * public final String getName():获取线程的名称. * 如何设置线程对象的名称呢? * public final void setName ...
- Python基础 — Pandas
Pandas -- 简介 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas ...
- bzoj 4719: [Noip2016]天天爱跑步【树上差分+dfs】
长久以来的心理阴影?但是其实非常简单-- 预处理出deep和每组st的lca,在这里我简单粗暴的拿树剖爆算了 然后考虑对于一组s t lca来说,被这组贡献的观察员x当且仅当: x在s到lca的路径上 ...
- 2017西安网络赛C_SUM
样例输入 1 1 样例输出 89999999999999999999999999 题意:利用上述公式,求出k的值 思路:找规律,找规律发现233个9,无论x是何值永远成立 (这种规律题尽量就不用跟队友 ...
- Html 标签的事件绑定(转自 MSDN)
参考 MSDN 网页给 HTML 标签绑定 click 事件: function makeFoldersCollapsible(folderIcon, openFolderIcon, pathToIc ...