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 ...
随机推荐
- SilverLight-DataBinding:二、Bingding to a Collection Objects(绑定一个集合对象)
ylbtech-SilverLight-DataBinding: Bingding to a Collection Objects(绑定一个集合对象) 1.A, Building a Data Ob ...
- Android GC 原理探究
导语 想写一篇关于 android GC 的想法来源于追查一个魅族手机图片滑动卡顿问题,由于不断的 GC 导致的丢帧卡顿的问题让我们想了很多方案去解决,所以就打算详细的看看内存分配和 GC 的原理,为 ...
- linux下脚本监控网络流量
linux下脚本监控网络流量 学习了:https://blog.csdn.net/chenghuikai/article/details/48437479 学习了:http://www.jb51.ne ...
- Hibernate中的条件查询完毕类
Hibernate中的条件查询有下面三个类完毕: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类
- IMDB-WIKI - 具有年龄和性别标签的500k +脸部图像
Rasmus Rothe, Radu Timofte, Luc Van Gool DEX:从单一形象深刻地看待年龄 观看 人物研讨会国际计算机视觉大会(ICCV),2015*获胜LAP面对年龄估计的挑 ...
- 在Linux的Eclipse下搭建Android环境
http://blog.csdn.net/lyonte/article/details/6407242 一.Java环境安装配置详见<在Linux下搭建Java环境>http://blog ...
- GreenPlum 安装方法详解
一.安装环境准备 1.磁盘环境准备 磁盘分区典型配置如下: 文件系统 文件格式 大小 / ext3 50GB,Linux系统的根目录,所有的目录都挂在这个目录下面,建议大小为 ...
- Java Base64加密、解密原理Java代码(转载)
博客来源:http://blog.csdn.net/songylwq/article/details/7578905 Base64是什么: Base64是网络上最常见的用于传输8Bit字节代码的编码方 ...
- toad for oracle中文显示乱码
toad for oracle中文显示乱码 数据入库的时候中文显示正常,在toad for oracleclient和页面显示都是乱码!!! 原因:在数据入库时候出现的问题. 解决方式: 在系统变量中 ...
- MVC——分页
添加类PageBar.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...