Codeforces #257B Jzzhu and Sequences(矩阵快速幂)
来源:互联网 发布:淘宝哪里取消申请退款 编辑:程序博客网 时间:2024/06/06 08:55
Description
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).
Sample Input
Input
2 33
Output
1
Input
0 -12
Output
1000000006
题比较简单,和求斐波那契数列一样,唯一要注意的就是前两项的输出。
值为负的时候要先转为正的再取模。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int MOD=1e9+7;const int INF = 0x3f3f3f3f;struct Matrix{ LL mat[2][2]; void Clear() { CLEAR(mat,0); }};Matrix mult(Matrix m1,Matrix m2){ Matrix ans; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { ans.mat[i][j]=0; for(int k=0;k<2;k++) { ans.mat[i][j]=(ans.mat[i][j]+m1.mat[i][k]*m2.mat[k][j]); while(ans.mat[i][j]<0) ans.mat[i][j]+=MOD; ans.mat[i][j]%=MOD; } } } return ans;}Matrix Pow(Matrix m1,LL b){ Matrix ans;ans.Clear(); for(int i=0;i<2;i++) ans.mat[i][i]=1; while(b) { if(b&1) ans=mult(ans,m1); b>>=1; m1=mult(m1,m1); } return ans;}LL x,y,n;int main(){ while(~scanf("%lld%lld",&x,&y)) { while(x<0) x+=MOD;while(y<0) y+=MOD; x%=MOD;y%=MOD; Matrix A;scanf("%lld",&n); if(n==1) { printf("%lld\n",x); continue; } if(n==2) { printf("%d\n",y); continue; } A.mat[0][0]=1;A.mat[0][1]=-1; A.mat[1][0]=1;A.mat[1][1]=0; A=Pow(A,n-2); LL ans=0; ans=A.mat[0][0]*y;while(ans<0) ans+=MOD; ans%=MOD;ans+=A.mat[0][1]*x;while(ans<0) ans+=MOD; ans%=MOD; printf("%lld\n",ans); } return 0;}
0 0
- Codeforces #257B Jzzhu and Sequences(矩阵快速幂)
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵快速幂)
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 450B-Jzzhu and Sequences (矩阵快速幂)
- codeforces 450-B Jzzhu and Sequences 矩阵快速幂
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- CF 450 B Jzzhu and Sequences(矩阵快速幂)
- Codeforces Round #257 (Div. 2) 450B - Jzzhu and Sequences(循环节或矩阵快速幂)
- 【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)
- Jzzhu and Sequences - CF 450B 矩阵快速幂版
- Codeforces #257 div2 B. Jzzhu and Sequences
- Jzzhu and Sequences 【矩阵快速幂】
- CodeForces450B Jzzhu and Sequences (矩阵快速幂)
- codeforces B. Jzzhu and Sequences
- Codeforces #257 (Div. 2) B. Jzzhu and Sequences
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences
- ubuntu下搜索文件
- 服务器运行的状态,修改Spring中的applicationContext.xml配置文件
- 多线程与多进程的对比
- 网站分析与优化
- 3.17
- Codeforces #257B Jzzhu and Sequences(矩阵快速幂)
- 程序猿的日常-5
- HDU 1016 - Prime Ring Problem [简单DFS]
- Letter Combinations of a Phone Number
- 《AngularJS权威教程》
- F - Flowers
- Android学习笔记の五
- 15 Wallpaper 之OnTouch OnIntercept dispatch事件机制
- Raising Modulo Numbers (poj 1995 快速幂水题)