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: ...
随机推荐
- linux进程间通信——netlink【转】
本文转载自:http://blog.csdn.net/wangyuling1234567890/article/details/21561457 今天在看用户态与内核态通信相关东西时,发现了关于net ...
- LA3704
https://vjudge.net/problem/UVALive-3704 参考:http://www.cnblogs.com/iwtwiioi/p/3946211.html 循环矩阵... 我们 ...
- 【WIP】数据结构与算法入门
创建: 2017/12/25
- bzoj 1575: [Usaco2009 Jan]气象牛Baric【dp】
完了不会dp了 设f[i][j]为以i结尾,有j个时的最优值,辅助数组g[i][j]为s选了i和j,i~j中的误差值 转移是f[j][i]=min(f[k][i-1]+g[k][j]) #includ ...
- react hooks 全面转换攻略(一) react本篇之useState,useEffect
useState 经典案例: import { useState } from 'react'; function Example() { const [count, setCount] = useS ...
- 关于Android皮肤更换分享
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=264902&highlight=%E6%8D%A2%E8%82%A4&a ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- 题解报告:hdu 1015 Safecracker
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kl ...
- EditText(3)输入时自动完成功能
在android输入自动完成功能由EditText的子类 AutoCompleteTextView 实现.如下: 1,在xml中使用 <AutoCompleteTextView android: ...
- Hadoop Hive概念学习系列之hive的索引及案例(八)
hive里的索引是什么? 索引是标准的数据库技术,hive 0.7版本之后支持索引.Hive提供有限的索引功能,这不像传统的关系型数据库那样有“键(key)”的概念,用户可以在某些列上创建索引来加速某 ...