UVa 10689 - Yet another Number Sequence
来源:互联网 发布:觉得自己很透明知乎 编辑:程序博客网 时间:2024/06/07 03:02
题目:给你Fib数列的前两项,求第n项的后m位的值。
分析:矩阵快速模幂。见本博客的:斐波那契数列
说明:╮(╯▽╰)╭。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;class matrix{private:int data[2][2];public:matrix(){};matrix(int a, int b, int c, int d){data[0][0] = a;data[0][1] = b;data[1][0] = c;data[1][1] = d;}matrix(int a, int b, int mod);friend matrix mul(matrix A, matrix B, int mod);friend matrix qpow(matrix mat, int n, int mod);int operator ()(int x, int y){return data[x-1][y-1];}};//矩阵乘法 matrix mul(matrix A, matrix B, int mod){matrix C;for (int i = 0 ; i < 2 ; ++ i)for (int j = 0 ; j < 2 ; ++ j) {C.data[i][j] = 0;for (int k = 0 ; k < 2 ; ++ k)C.data[i][j] = (C.data[i][j]+A.data[i][k]*B.data[k][j])%mod;}return C;}//矩阵快速幂 matrix qpow(matrix mat, int n, int mod){if (n == 1) return mat;matrix now = qpow(mat, n/2, mod);if (n%2 == 0) return mul(now, now, mod);return mul(mul(now, now, mod), mat, mod);}int mod[5] = {1, 10, 100, 1000, 10000};int main(){int t,a,b,n,m;while (cin >> t)while (t --) {cin >> a >> b >> n >> m;if (n == 0) cout << a%mod[m] << endl;else if (n == 1) cout << b%mod[m] << endl;else if (n == 2) cout << (a+b)%mod[m] << endl;else {matrix A(a, b, b, a+b);matrix B = qpow(matrix(0, 1, 1, 1), n-2, mod[m]);cout << mul(A, B, mod[m])(2,2) << endl;}} return 0;}
0 0
- UVa 10689 - Yet another Number Sequence
- UVA 10689 Yet another Number Sequence
- UVA 10689 Yet another Number Sequence
- Yet another Number Sequence UVA
- uva 10689 - Yet another Number Sequence(缓存,类Fibonacci)
- UVA 10689 - Yet another Number Sequence(矩阵快速幂)
- uva 10689 - Yet another Number Sequence(矩阵快速幂)
- uva 10689 Yet another Number Sequence【矩阵快速幂】
- UVA 10689 Yet another Number Sequence(Fibonacci数列)
- 【矩阵快速幂】UVA 10698 G - Yet another Number Sequence
- UVA 10698 Yet another Number Sequence 矩阵快速幂
- uva10689 Yet another Number Sequence
- UVA10689-Yet another Number Sequence
- Yet Another Number Sequence CodeForces
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- Codeforces 392C Yet Another Number Sequence
- Codeforces 392C Yet Another Number Sequence
- 数论基础:矩阵快速幂解斐波那契数列Yet another Number Sequence(UVA)
- SHELL学习笔记2
- 自定义ADT创建项目向导
- RHEL/CentOS 7.x的几点新改变
- 为什么ios模拟器出现不了内存不足的问题,而真机会
- STL中set用法详解
- UVa 10689 - Yet another Number Sequence
- 1144: 组合的输出
- Java-Java语言基础
- Linux下通过crontab及expect实现自动化处理
- Majority Element
- Oracle APEX 4.2安装 与Oracle PL/SQL Gateway的配置
- 主从错误UUID
- 【数据结构】堆heap,Trie树,位图Bitmap
- Linux route 使用