矩阵快速幂。

首先得到公式

然后构造矩阵,用矩阵加速

取模函数需要自己写一下,是数论中的取模。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. long long MOD = 1e9 + ;
  9. long long x, y;
  10. int n;
  11.  
  12. long long mod(long long a, long long b)
  13. {
  14. if (a >= ) return a%b;
  15. if (abs(a) % b == ) return ;
  16. return (a + b*(abs(a) / b + ));
  17. }
  18.  
  19. struct Matrix
  20. {
  21. long long A[][];
  22. int R, C;
  23. Matrix operator*(Matrix b);
  24. };
  25.  
  26. Matrix X, Y, Z;
  27.  
  28. Matrix Matrix::operator*(Matrix b)
  29. {
  30. Matrix c;
  31. memset(c.A, , sizeof(c.A));
  32. int i, j, k;
  33. for (i = ; i <= R; i++)
  34. for (j = ; j <= C; j++)
  35. for (k = ; k <= C; k++)
  36. c.A[i][j] = mod((c.A[i][j] + mod(A[i][k] * b.A[k][j], MOD)), MOD);
  37. c.R=R; c.C=b.C;
  38. return c;
  39. }
  40.  
  41. void read()
  42. {
  43. scanf("%lld%lld%d", &x, &y, &n);
  44. }
  45.  
  46. void init()
  47. {
  48. n = n - ;
  49. Z.A[][] = x, Z.A[][] = y; Z.R = ; Z.C = ;
  50. Y.A[][] = , Y.A[][] = , Y.A[][] = , Y.A[][] = ; Y.R = ; Y.C = ;
  51. X.A[][] = , X.A[][] = -, X.A[][] = , X.A[][] = ; X.R = ; X.C = ;
  52. }
  53.  
  54. void work()
  55. {
  56. while (n)
  57. {
  58. if (n % == ) Y = Y*X;
  59. n = n >> ;
  60. X = X*X;
  61. }
  62. Z = Z*Y;
  63.  
  64. printf("%lld\n", mod(Z.A[][], MOD));
  65. }
  66.  
  67. int main()
  68. {
  69. read();
  70. init();
  71. work();
  72. return ;
  73. }

CodeForces 450B Jzzhu and Sequences的更多相关文章

  1. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  2. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  3. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  4. codeforces 450B. Jzzhu and Sequences 解题报告

    题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...

  5. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  6. CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解

    思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...

  7. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  8. Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

  9. Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. swool安装

    swoole安装 安装完PHP后,即可安装swoole扩展. swoole扩展下载地址:https://github.com/swoole/swoole-src/releases 尽量选择stable ...

  2. OpenGL------显示列表

    我们已经知道,使用OpenGL其实只要调用一系列的OpenGL函数就可以了.然而,这种方式在一些时候可能导致问题.比如某个画面中,使用了数千个多边形来表现一个比较真实的人物,OpenGL为了产生这数千 ...

  3. RING0,RING1,RING2,RING3

    Intel的CPU将特权级别分为4个级别:RING0,RING1,RING2,RING3.Windows只使用其中的两个级别RING0和RING3,RING0只给操作系统用,RING3谁都能用.如果普 ...

  4. NPAPI简介

    原文出处:http://blog.csdn.net/wwwsq/article/details/8254911 NPAPI是电脑上大部分非ie浏览器都支持的一种浏览器扩展,类似ie的activex技术 ...

  5. mysql 数字字段的类型选择

    bigint            从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8  ...

  6. My workbench draft

    System Linux / Ubuntu 14.04.5 LTS (Trusty Tahr) + ROS Indigo Linux / Ubuntu 16.04.1 LTS (Xenial Xeru ...

  7. zendstudio的安装与配置

    <微信公众平台应用开发实战>第1章搭建开发环境和相关技术介绍,本章会先介绍微信公众平台的一些基本概念和公众平台的开发模式:然后讲解如何搭建开发环境—AppServ和zendstudio:然 ...

  8. storm的并发

    1 storm并行的基本概念 storm集群中的一个机器可以运行一个或者多个worker,对应于一个或者多个topologies. 1个worker进程运行1个或多个excutor线程.每个worke ...

  9. Eclipse中代码提示框的背景色修改

    Preferences->General->Appearance->Colors and Fonts 修改Basic中的Content Assist backgroud color[ ...

  10. 转载 C++学习第9篇---类和类的封装

    http://blog.csdn.net/zuheyawen/article/details/7324340