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: ...
随机推荐
- windows console Kill PID 端口查看
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- Centos7 防火墙firewalld配置
开启80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 出现success表明添加成功 移除某个端口 firewall-cmd ...
- SemaphoreSlim
https://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.110).aspx Represents a ...
- YTU 2520: 小慧唱卡拉OK
2520: 小慧唱卡拉OK 时间限制: 1 Sec 内存限制: 128 MB 提交: 478 解决: 207 题目描述 小慧唱歌非常好听,小鑫很喜欢听小慧唱歌,小鑫最近又想听小慧唱歌了,于是小鑫请 ...
- 深入理解JMM(Java内存模型) --(七)总结
JMM 掌管着一个线程对内存的动作 (读和写)影响其他线程对内存的动作的方式.由于使用处理器寄存器和预处理 cache 来提高内存访问速度带来的性能提升,Java 语言规范(JLS)允许一些内存操作并 ...
- openStack集群封装记录
- 在 Vue 项目中(vue-cli2,vue-cli3)使用 pug 简化 HTML 的编写
使用 pug 的原因: 使得 HTML 写起了来更加清晰和快捷 用法: Vue 的用法没有变化: <template lang="pug"> transition(na ...
- 04-Vue中的动画
Vue中的动画 为什么要有动画:动画能够提高用户的体验,帮助用户更好的理解页面中的功能: -使用过渡类名 1.html <div id="app"> <input ...
- [Usaco2018 Open]Milking Order
Description Farmer John的N头奶牛(1≤N≤10^5),仍然编号为1-N,正好闲得发慌.因此,她们发展了一个与Farmer John每天早上为她们挤牛奶的时候的排队顺序相关的复杂 ...
- cloudera-agent启动File not found : /usr/sbin/cmf-agent解决办法(图文详解)
不多说,直接上干货! 问题详情 bigdata@nssa-sensor1:~$ sudo service cloudera-scm-agent startFile not found : /usr/s ...