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 ...
随机推荐
- Oracle SOA Suit Adapter
SOA架构的一个核心的使命是整合企业现存的各式各样的计算资源,它不仅仅是代码层面的整合,更是硬件,计算能力,服务能力的整合.Oracle SOA Suite在这方面做得特别的贴切,它提供了一组Adap ...
- [反汇编练习] 160个CrackMe之033
[反汇编练习] 160个CrackMe之033. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 机器学习(十三)——机器学习中的矩阵方法(3)病态矩阵、协同过滤的ALS算法(1)
http://antkillerfarm.github.io/ 向量的范数(续) 范数可用符号∥x∥λ表示. 经常使用的有: ∥x∥1=|x1|+⋯+|xn| ∥x∥2=x21+⋯+x2n−−−−−− ...
- SolidEdge 打开工程图提示图纸已过期怎么办
如下图所示,打开工程图时提示图纸已过期 点击工具-图纸视图跟踪器,按提示打开过期的装配体文件 更新这个装配体文件 然后切换到刚才提示过期的工程图文件,点击更新视图,下次再打开的时候就不会提 ...
- Vue2.0 视频教程
好像是一套vue 开发webapp 课程.来自网络. url:https://pan.baidu.com/s/1jIele9w password:b404 文章来源:刘俊涛的博客 地址:http:// ...
- 不错.net图片水印类
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...
- Effective C++ 条款一 视C++为一个语音联邦
1.C语言 区块.语句.预处理器.内置数据类型.数组.指针等内容 2.OC++ 类.封装.继承.多态.virtual函数 等 3.Template C++ 泛型 ...
- python(35)- 异常处理
一 错误和异常 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 ...
- web微信开发
群里接收消息时,使用广播,但需要刷新页面才能接收到广播内容. - 轮询: 定时每秒刷新一次,当群不活跃时,群里的每个客户端都在刷新,对服务端压力太大. - 长轮询:客户端连服务端,服务端一直不断开,也 ...
- idea安装plugin
---恢复内容开始--- ---恢复内容结束---