codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接:
1 second
256 megabytes
standard input
standard output
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output a single integer representing fn modulo 1000000007 (109 + 7).
2 3
3
1
0 -1
2
1000000006
In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.
In the second sample, f2 = - 1; - 1 modulo (10^9 + 7) equals (10^9 + 6).
题意:
水题,不行说;
思路:
矩阵快速幂的水题;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const ll mod=1e9+;
ll n,x,y;
struct matrix
{
ll a[][];
};
matrix mul(matrix A,matrix B)
{
matrix s;
s.a[][]=s.a[][]=;
s.a[][]=s.a[][]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
s.a[i][j]=;
for(int k=;k<;k++)
{
s.a[i][j]+=A.a[i][k]*B.a[k][j];
s.a[i][j]%=mod;
}
}
}
return s;
}
ll fast_pow(matrix A,ll num)
{
matrix s,base;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
s.a[i][j]=(i==j);
base.a[i][j]=A.a[i][j];
}
} while(num)
{
if(num&)
{
s=mul(s,base);
}
base=mul(base,base);
num=(num>>);
}
return (s.a[][]*y%mod+s.a[][]*x%mod)%mod; } int main()
{
cin>>x>>y;
cin>>n;
matrix ma;
ma.a[][]=ma.a[][]=;
ma.a[][]=-;
ma.a[][]=;
if(n>)cout<<(fast_pow(ma,n-)%mod+mod)%mod<<"\n";
else if(n==)cout<<(y%mod+mod)%mod<<"\n";
else cout<<(x%mod+mod)%mod<<"\n"; }
codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)的更多相关文章
- 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 ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- Codeforces 514E Darth Vader and Tree 矩阵快速幂
Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...
- Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP
题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- Codeforces 696D Legen...(AC自动机 + 矩阵快速幂)
题目大概说给几个字符串,每个字符串都有一个开心值,一个串如果包含一次这些字符串就加上对应的开心值,问长度n的串开心值最多可以是多少. POJ2778..复习下..太弱了都快不会做了.. 这个矩阵的乘法 ...
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
随机推荐
- java线程中Exchanger使用
有时我们须要对元素进行配对和交换线程的同步点,使用exchange方法 返回其伙伴的对象,这时我们就须要使用线程类中的Exchanger类了, 我通过一个实例 来简单说明一下他的用法及其作用: imp ...
- procomm plus
procomm plus这是查看串口数据的软件.
- UNIX&Linux发展图谱
来自为知笔记(Wiz)
- nodejs 打印机打印 pos打印
https://www.npmjs.com/package/chn-escpos 安装window vsbuild 编译工具 npm install --global --production win ...
- LinearLayout具体解释三:LayoutInflater创建View过程分析
上次讲到以下这么一段代码,这段代码的作用就是解析xml文件成为view并显示到屏幕上的. @Override //设置contentview,也就是activity或fragment载入视图,即vie ...
- windows环境下生成ssh keys
参考:https://www.cnblogs.com/achengmu/p/6095046.html 1.首先你要安装Git工具 2.运行Git Bash here 3.输入指令,进入.ssh文件夹 ...
- Java中HashMap遍历的两种方法(转)
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- 分享ArcGIS Server 10.0修复安装心得
最近,捣腾了一阵子在xp系统上安装ArcGIS Server10.0(下方均简称server),解决了一些初学者可能面临的problem,给大家贴出来, 希望能够给初学者一些有益的帮助. 我的系统环境 ...
- 负载均衡之F5设备
http://xjsunjie.blog.51cto.com/999372/666672 目前全球范围内应用比较广泛的负载均衡设备为美国的F5.F5于2000年底进驻中国,在国内业界,F5负载均衡产品 ...
- mysql 海量数据删除
百度知道 - mysql删除海量数据 MySQL 数据库删除大批量数据的优化 看到这儿的话,最后看下这篇文章,对于操作海量数据的sql深入分析 cnblogs - 深度分析DROP,TRU ...