题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数。

分析:n最大为109,矩阵快速幂求解,复杂度log2(109)。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<iostream>
  7. #include<sstream>
  8. #include<iterator>
  9. #include<algorithm>
  10. #include<string>
  11. #include<vector>
  12. #include<set>
  13. #include<map>
  14. #include<stack>
  15. #include<deque>
  16. #include<queue>
  17. #include<list>
  18. #define lowbit(x) (x & (-x))
  19. const double eps = 1e-9;
  20. inline int dcmp(double a, double b){
  21. if(fabs(a - b) < eps) return 0;
  22. return a > b ? 1 : -1;
  23. }
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. const int INT_INF = 0x3f3f3f3f;
  27. const int INT_M_INF = 0x7f7f7f7f;
  28. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  29. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  30. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  31. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  32. const int MOD = 1000007;
  33. const double pi = acos(-1.0);
  34. const int MAXN = 400 + 10;
  35. const int MAXT = 1000 + 10;
  36. using namespace std;
  37. int POW[10];
  38. int a, b, n, m;
  39. void init(){
  40. POW[1] = 10;
  41. for(int i = 2; i <= 4; ++i){
  42. POW[i] = POW[i - 1] * 10;
  43. }
  44. }
  45. struct Matrix{
  46. int r, c;
  47. int matrix[2][2];
  48. Matrix(int rr, int cc):r(rr), c(cc){
  49. memset(matrix, 0, sizeof matrix);
  50. }
  51. };
  52. Matrix mul(Matrix a, Matrix b){
  53. Matrix ans(a.r, b.c);
  54. for(int i = 0; i < a.r; ++i){
  55. for(int j = 0; j < b.c; ++j){
  56. int tmp = 0;
  57. for(int k = 0; k < a.c; ++k){
  58. (tmp += a.matrix[i][k] * b.matrix[k][j]) %= POW[m];
  59. }
  60. ans.matrix[i][j] = tmp;
  61. }
  62. }
  63. return ans;
  64. }
  65. Matrix QPOW(Matrix tmp, int k){
  66. Matrix ans(2, 2);
  67. ans.matrix[0][0] = ans.matrix[1][1] = 1;
  68. while(k){
  69. if(k & 1) ans = mul(ans, tmp);
  70. tmp = mul(tmp, tmp);
  71. k >>= 1;
  72. }
  73. return ans;
  74. }
  75. int main(){
  76. int T;
  77. scanf("%d", &T);
  78. init();
  79. while(T--){
  80. scanf("%d%d%d%d", &a, &b, &n, &m);
  81. Matrix tmp1(2, 2), tmp2(2, 1);
  82. tmp1.matrix[0][0] = tmp1.matrix[0][1] = tmp1.matrix[1][0] = 1;
  83. tmp2.matrix[0][0] = b;
  84. tmp2.matrix[1][0] = a;
  85. Matrix ans = mul(QPOW(tmp1, n - 1), tmp2);
  86. printf("%d\n", ans.matrix[0][0]);
  87. }
  88. return 0;
  89. }

  

UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)的更多相关文章

  1. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  2. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  3. poj3070矩阵快速幂求斐波那契数列

      Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 9368 Desc ...

  4. UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  5. 51 Nod 1242 矩阵快速幂求斐波那契数列

    #include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; type ...

  6. 矩阵快速幂 求斐波那契第N项

    #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> us ...

  7. 矩阵快速幂--51nod-1242斐波那契数列的第N项

    斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...

  8. codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...

  9. python 快速幂求斐波那契数列

    先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...

随机推荐

  1. Red_Hat yum源配置

    http://www.linuxidc.com/Linux/2016-06/132171.htm

  2. 多选按钮CheckBox

    main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  3. 091、Java中String类之使用“==”比较

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. C语言常用函数

    一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...

  5. 你必须知道的.Net 8.2.2 本质分析

    1 .Equals  静态方法  Equals 静态方法实现了对两个对象的相等性判别,其在 System.Object 类型中实现过程可以表 示为: public static bool Equals ...

  6. Docker基本使用运行ngix镜像

    docker pull 项目名 会从docker默认的仓库去拉去项目,如果是docker pull 项目名 地址 会从给定地址拉去镜像 docker run image名字  运行镜像 docker架 ...

  7. 四篇关于chen_zhe的美文

    壹   chen_zhe人 那是谁 是谁 是谁 那就是 chen_zhe chen_zhe人 chen_zhe人 背负着暴政之名 抛弃了一切(指民心)而战斗(指禁言)的男人 chen_zhe代码是超音 ...

  8. 题解:luogu P1247

    大概没你们说得复杂吧...... \(Part\;1\) \(Nim\)游戏 大家都对异或和感到懵逼吧(排除大佬),其实很简单,用\(SG\)函数打表计算即可解决: 抛个板子: void get_sg ...

  9. Ubuntu 更新源 内核升级

    cat /etc/apt/sources.listdeb http://mirrors.sohu.com/ubuntu/ precise main restricted universe multiv ...

  10. grep -o -E

    sed 命令可以很好的进行行匹配,但从某一行中精确匹配某些内容,则使用 grep 命令并辅以 -o 和 -E 选项可达到此目的.其中 -o 表示“only-matching”,即“仅匹配”之意.光用它 ...