LightOJ 1065 Number Sequence(矩阵快速幂)
来源:互联网 发布:如何理解大数据运用 编辑:程序博客网 时间:2024/06/01 09:30
题意:
就是用矩阵快速幂求斐波那契数列并取模。顺便学了一下矩阵快速幂。至于矩阵快速幂是什么就去看看讲解的博客吧。
代码:
//// Created by CQU_CST_WuErli// Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.//#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define SI(a) scanf("%d",&a)#define SII(a,b) scanf("%d%d",&a,&b)#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)#define Rep(flag,start,end) for(int flag=start;flag>=end;flag--)#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Root 1,n,1#define BigInteger bignconst int MAX_L=2005;// For BigIntegerconst int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7fffffff;const int MOD=1e9+7;const double eps=1e-9;const double pi=acos(-1);typedef long long ll;using namespace std;int mod;int a,b,n,m;struct Matrix { int mat[2][2];}initM,ansM;Matrix multi(Matrix& a,Matrix& b) { Matrix ret; rep(i,0,1) rep(j,0,1) { ret.mat[i][j]=0; rep(k,0,1) ret.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod; ret.mat[i][j]=ret.mat[i][j]%mod; } return ret;}void calc(int n) { ansM.mat[0][0]=1; ansM.mat[0][1]=0; ansM.mat[1][0]=0; ansM.mat[1][1]=1; initM.mat[0][0]=0; initM.mat[0][1]=1; initM.mat[1][0]=1; initM.mat[1][1]=1; while (n) { if (n&1) ansM=multi(initM,ansM); initM=multi(initM,initM); n>>=1; }}int main(int argc, char const *argv[]) {#ifdef LOCAL freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif for (int T_T,kase=SI(T_T);kase<=T_T;kase++) { SII(a,b); SII(n,m); mod=1; rep(i,1,m) mod*=10; cout << "Case " << kase << ": "; if (n==0) cout << a%mod; else if (n==1) cout << b%mod; else if (n==2) cout << (a+b)%mod; else { calc(n-1); cout << ((ansM.mat[1][0]*a)%mod+(ansM.mat[1][1]*b)%mod)%mod << endl; } } return 0;}
0 0
- LightOJ 1065 Number Sequence(矩阵快速幂)
- HDU1005 - Number Sequence (矩阵快速幂)
- Number Sequence 矩阵快速幂
- hdu 1005Number Sequence (矩阵快速幂)
- ZOJ 2105 Number Sequence(矩阵快速幂)
- 【HDOJ 1005】 Number Sequence (裸矩阵快速幂)
- HDU 1005 Number Sequence(矩阵快速幂)
- HDU 1005 Number Sequence(基础矩阵快速幂)
- 7_22_G Yet another Number Sequence(矩阵快速幂)
- HDU-1005 Number Sequence(矩阵快速幂或模拟)
- Hdu 1005 Number Sequence//矩阵快速幂
- ZOJ 2105 Number Sequence(矩阵快速幂)
- 【HDU1005】Number Sequence(矩阵快速幂)
- HDU 1005 Number Sequence(矩阵快速幂)
- HDU-1005- Number Sequence (矩阵快速幂)
- HDU1005-Number Sequence-矩阵快速幂
- HDU 1005 Number Sequence 矩阵快速幂
- hdu 1005 Number Sequence 构造矩阵 + 矩阵快速幂
- codeforces 629C
- NYOJ 某种序列
- POJ 3746 Teacher YYF(恶心+打表)
- Leetcode ☞ 217. Contains Duplicate
- 安卓第四天笔记-Sqlite
- LightOJ 1065 Number Sequence(矩阵快速幂)
- TCP/IP各层协议介绍
- lintcdoe: Number of Airplanes in the Sky
- 计蒜客 挑战难题 批量替换字符串
- Selenium的API介绍
- notepad快速解决一个小问题
- 短信还原的注意点
- VB.net 监视Windows剪切板中数据变化
- 看JDBC轻松连接数据库,你值得拥有