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: ...
随机推荐
- 大文本 通过 hadoop spark map reduce 获取 特征列 的 属性值 计算速度
大文本 通过 hadoop spark map reduce 获取 特征列 的 属性值 计算速度
- 获取特定html源码 富文本编辑器 爬虫生成 dom
python beautifulsoup获取特定html源码 - 吴悟无 - 博客园 https://www.cnblogs.com/vickey-wu/p/6843411.html PyQuery库 ...
- 【NOIP1999】【Codevs 1046】旅行家的预算
http://codevs.cn/problem/1046/ Solution: 贪心,如果当前油价很低,它就比起当前剩余油的价还低就可以替换,并且每次加满,最后把剩的油卖掉即可 油价用单调链表(不知 ...
- Hadoop安装配置(ubuntu-12.04.2-server-amd64)
环境如下: ubuntu-12.04.2-server-amd64 hadoop-1.0.4 VirtualBox 1.在VBox中安装Ubuntu Server,用户名和密码都是hadoop,安装完 ...
- 洛谷 P1970 花匠 —— DP
题目:https://www.luogu.org/problemnew/show/P1970 普通的DP,f[i][0/1] 表示 i 处处于较小或较大的长度: 注意:1.树状数组向后 query 时 ...
- Oracle强杀进程
1.找到sid,serial#: SELECT /*+ rule */ s.username, l.type, decode(l.type,'TM','TABLE LOCK', ...
- Spark 多项式逻辑回归__多分类
package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{B ...
- DNS的主从、子域授权和转发服务器
DNS的主从.子域授权和转发服务器 主从DNS 注意: 1.全局配置options{} 里面的内容,其中 listen-on port 53 {any or local:}:或者直接注释掉,或删掉 a ...
- bzoj 1603: [Usaco2008 Oct]打谷机【瞎搞】
一棵树,碰到改变转向的边就异或一下,从1dfs一遍 #include<iostream> #include<cstdio> using namespace std; const ...
- /usr/lib64/python2.6/lib-dynload/pyexpat.so: symbol XML_SetHashSalt, version EXPAT_2_0_1_RH not defined in file libexpat.so.1 with link time reference
解决方法:[root]$cd /usr/lib64/python2.6/lib-dynload[root]$ln -s /lib64/libexpat.so.1.5.2 libexpat.so.0[r ...