codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
2016-04-19 23:03
393 查看
题目链接:
B. Jzzhu and Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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).
Input
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
Output a single integer representing fn modulo 1000000007 (109 + 7).
Examples
input
output
input
output
Note
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代码:
B. Jzzhu and Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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).
Input
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
Output a single integer representing fn modulo 1000000007 (109 + 7).
Examples
input
2 3 3
output
1
input
0 -1 2
output
1000000006
Note
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+6; typedef long long ll; const ll mod=1e9+7; ll n,x,y; struct matrix { ll a[2][2]; }; matrix mul(matrix A,matrix B) { matrix s; s.a[0][0]=s.a[1][1]=0; s.a[0][1]=s.a[1][0]=0; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { s.a[i][j]=0; for(int k=0;k<2;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=0;i<2;i++) { for(int j=0;j<2;j++) { s.a[i][j]=(i==j); base.a[i][j]=A.a[i][j]; } } while(num) { if(num&1) { s=mul(s,base); } base=mul(base,base); num=(num>>1); } return (s.a[0][0]*y%mod+s.a[0][1]*x%mod)%mod; } int main() { cin>>x>>y; cin>>n; matrix ma; ma.a[0][0]=ma.a[1][0]=1; ma.a[0][1]=-1; ma.a[1][1]=0; if(n>2)cout<<(fast_pow(ma,n-2)%mod+mod)%mod<<"\n"; else if(n==2)cout<<(y%mod+mod)%mod<<"\n"; else cout<<(x%mod+mod)%mod<<"\n"; }
相关文章推荐
- Scala进阶源码实战之五——List、Queue、Stack、Set、Map
- LINQ之路 8: 解释查询(Interpreted Queries)
- IDEA 自动生成 serialVersionUID
- vector去重--unique
- 欢迎使用CSDN-markdown编辑器
- hdu_5667_sequence
- GUI——AWT框架和容器,创建简单窗体
- UISrollView上嵌套UISlider
- javaweb中的request对象
- javaWeb中的request对象
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- Iwfu-保存Fragment切换时的UI状态/保存销毁Activity后再次进入的视图状态
- 这次栽倒在sscanf函数上------ 看看错误的语句:int nRet = sscanf("xxx=yyy", "%s=%s", szKey, szValue);
- SOAPUI笔记2:SoapUI widow破解
- queue_01
- executeScalar ExecuteNonQuery
- jdgui反编译+javac编译=无源文件改动代码
- unique()去重函数
- iOS 动画Animation - 5:UIBezier
- ZOJ 2136 Longest Ordered Subsequence