题目链接:https://www.luogu.org/problemnew/show/P1962

题目链接:https://www.nowcoder.com/practice/376282a6682a4005973cde7b3df69584?tpId=101&tqId=33251&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目链接:https://www.nowcoder.com/practice/fd66768dc08748f2be6626f07a02e466?tpId=101&tqId=33252&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目大意:

  略

分析:

  由于数据规模很大,需要用矩阵快速幂来解。

代码如下:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
  6. #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
  7. #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
  8. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12.  
  13. #define pr(x) cout << #x << " = " << x << " "
  14. #define prln(x) cout << #x << " = " << x << endl
  15.  
  16. #define LOWBIT(x) ((x)&(-x))
  17.  
  18. #define ALL(x) x.begin(),x.end()
  19. #define INS(x) inserter(x,x.begin())
  20. #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
  22. #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24.  
  25. #define ms0(a) memset(a,0,sizeof(a))
  26. #define msI(a) memset(a,0x3f,sizeof(a))
  27. #define msM(a) memset(a,-1,sizeof(a))
  28.  
  29. #define MP make_pair
  30. #define PB push_back
  31. #define ft first
  32. #define sd second
  33.  
  34. template<typename T1, typename T2>
  35. istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. in >> p.first >> p.second;
  37. return in;
  38. }
  39.  
  40. template<typename T>
  41. istream &operator>>(istream &in, vector<T> &v) {
  42. for (auto &x: v)
  43. in >> x;
  44. return in;
  45. }
  46.  
  47. template<typename T>
  48. ostream &operator<<(ostream &out, vector<T> &v) {
  49. Rep(i, v.size()) out << v[i] << " \n"[i == v.size() - ];
  50. return out;
  51. }
  52.  
  53. template<typename T1, typename T2>
  54. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. return out;
  57. }
  58.  
  59. inline int gc(){
  60. static const int BUF = 1e7;
  61. static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62.  
  63. if(bg == ed) fread(bg = buf, , BUF, stdin);
  64. return *bg++;
  65. }
  66.  
  67. inline int ri(){
  68. int x = , f = , c = gc();
  69. for(; c<||c>; f = c=='-'?-:f, c=gc());
  70. for(; c>&&c<; x = x* + c - , c=gc());
  71. return x*f;
  72. }
  73.  
  74. template<class T>
  75. inline string toString(T x) {
  76. ostringstream sout;
  77. sout << x;
  78. return sout.str();
  79. }
  80.  
  81. inline int toInt(string s) {
  82. int v;
  83. istringstream sin(s);
  84. sin >> v;
  85. return v;
  86. }
  87.  
  88. //min <= aim <= max
  89. template<typename T>
  90. inline bool BETWEEN(const T aim, const T min, const T max) {
  91. return min <= aim && aim <= max;
  92. }
  93.  
  94. typedef unsigned int uI;
  95. typedef long long LL;
  96. typedef unsigned long long uLL;
  97. typedef vector< int > VI;
  98. typedef vector< bool > VB;
  99. typedef vector< char > VC;
  100. typedef vector< double > VD;
  101. typedef vector< string > VS;
  102. typedef vector< LL > VL;
  103. typedef vector< VI > VVI;
  104. typedef vector< VB > VVB;
  105. typedef vector< VS > VVS;
  106. typedef vector< VL > VVL;
  107. typedef vector< VVI > VVVI;
  108. typedef vector< VVL > VVVL;
  109. typedef pair< int, int > PII;
  110. typedef pair< LL, LL > PLL;
  111. typedef pair< int, string > PIS;
  112. typedef pair< string, int > PSI;
  113. typedef pair< string, string > PSS;
  114. typedef pair< double, double > PDD;
  115. typedef vector< PII > VPII;
  116. typedef vector< PLL > VPLL;
  117. typedef vector< VPII > VVPII;
  118. typedef vector< VPLL > VVPLL;
  119. typedef vector< VS > VVS;
  120. typedef map< int, int > MII;
  121. typedef unordered_map< int, int > uMII;
  122. typedef map< LL, LL > MLL;
  123. typedef map< string, int > MSI;
  124. typedef map< int, string > MIS;
  125. typedef set< int > SI;
  126. typedef stack< int > SKI;
  127. typedef deque< int > DQI;
  128. typedef queue< int > QI;
  129. typedef priority_queue< int > PQIMax;
  130. typedef priority_queue< int, VI, greater< int > > PQIMin;
  131. const double EPS = 1e-;
  132. const LL inf = 0x7fffffff;
  133. const LL infLL = 0x7fffffffffffffffLL;
  134. const LL mod = 1e9 + ;
  135. const int maxN = 1e6 + ;
  136. const LL ONE = ;
  137. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  138. const LL oddBits = 0x5555555555555555;
  139.  
  140. struct Matrix{
  141. int row, col;
  142. LL MOD;
  143. VVL mat;
  144.  
  145. Matrix(int r, int c, LL p = mod) : row(r), col(c), MOD(p) {
  146. mat.assign(r, VL(c, ));
  147. }
  148. Matrix(const Matrix &x, LL p = mod) : MOD(p){
  149. mat = x.mat;
  150. row = x.row;
  151. col = x.col;
  152. }
  153. Matrix(const VVL &A, LL p = mod) : MOD(p){
  154. mat = A;
  155. row = A.size();
  156. col = A[].size();
  157. }
  158.  
  159. // x * 单位阵
  160. inline void E(int x = ) {
  161. assert(row == col);
  162. Rep(i, row) mat[i][i] = x;
  163. }
  164.  
  165. inline VL& operator[] (int x) {
  166. assert(x >= && x < row);
  167. return mat[x];
  168. }
  169.  
  170. inline Matrix operator= (const VVL &x) {
  171. row = x.size();
  172. col = x[].size();
  173. mat = x;
  174. return *this;
  175. }
  176.  
  177. inline Matrix operator+ (const Matrix &x) {
  178. assert(row == x.row && col == x.col);
  179. Matrix ret(row, col);
  180. Rep(i, row) {
  181. Rep(j, col) {
  182. ret.mat[i][j] = mat[i][j] + x.mat[i][j];
  183. ret.mat[i][j] %= MOD;
  184. }
  185. }
  186. return ret;
  187. }
  188.  
  189. inline Matrix operator* (const Matrix &x) {
  190. assert(col == x.row);
  191. Matrix ret(row, x.col);
  192. Rep(k, x.col) {
  193. Rep(i, row) {
  194. if(mat[i][k] == ) continue;
  195. Rep(j, x.col) {
  196. ret.mat[i][j] += mat[i][k] * x.mat[k][j];
  197. ret.mat[i][j] %= MOD;
  198. }
  199. }
  200. }
  201. return ret;
  202. }
  203.  
  204. inline Matrix operator*= (const Matrix &x) { return *this = *this * x; }
  205. inline Matrix operator+= (const Matrix &x) { return *this = *this + x; }
  206. };
  207.  
  208. // 矩阵快速幂,计算x^y
  209. inline Matrix mat_pow_mod(Matrix x, LL y) {
  210. Matrix ret(x.row, x.col);
  211. ret.E();
  212. while(y){
  213. if(y & ) ret *= x;
  214. x *= x;
  215. y >>= ;
  216. }
  217. return ret;
  218. }
  219.  
  220. LL N;
  221. Matrix ans(VVL({{, }, {, }}));
  222.  
  223. int main(){
  224. //freopen("MyOutput.txt","w",stdout);
  225. //freopen("input.txt","r",stdin);
  226. //INIT();
  227. scanf("%lld", &N);
  228. ans = mat_pow_mod(ans, N);
  229. printf("%lld\n", ans.mat[][]);
  230. return ;
  231. }

洛谷 P1962 斐波那契数列的更多相关文章

  1. 洛谷P1962 斐波那契数列【矩阵运算】

    洛谷P1962 斐波那契数列[矩阵运算] 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) ( ...

  2. 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]

    P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...

  3. 洛谷——P1962 斐波那契数列

    P1962 斐波那契数列 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 ...

  4. 洛谷—— P1962 斐波那契数列

    https://www.luogu.org/problem/show?pid=1962 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f ...

  5. 洛谷P1962 斐波那契数列(矩阵快速幂)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...

  6. 洛谷P1962 斐波那契数列题解

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...

  7. 【洛谷P1962 斐波那契数列】矩阵快速幂+数学推导

    来提供两个正确的做法: 斐波那契数列双倍项的做法(附加证明) 矩阵快速幂 一.双倍项做法 在偶然之中,在百度中翻到了有关于斐波那契数列的词条(传送门),那么我们可以发现一个这个规律$ \frac{F_ ...

  8. 题解——洛谷P1962 斐波那契数列(矩阵乘法)

    矩阵乘法加速线性递推的典型 大概套路就是先构造一个矩阵\( F \)使得另一初始矩阵\( A \)乘以\( F^{x} \)能够得出第n项 跑的飞快 虽然我也不知道那个矩阵要怎么构造 或许就像我使用了 ...

  9. 洛谷P1962 斐波那契数列

    传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...

随机推荐

  1. java~springboot~ibatis数组in查询的实现

    在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如where in这种从数组里查询符合条件的集合里,需要在xml ...

  2. MySQL系列--3.数据类型和连接查询

    1.存储引擎 数据创建,查询,更新和删除操作都是通过数据引擎来进行的.不同的存储引擎存储限制不同,支持不同的索引机制等. 查询数据库支持的存储引擎 MySQL 5.7.2支持的存储引擎有:InnoDB ...

  3. spring boot 集成 zookeeper 搭建微服务架构

    PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...

  4. [转]Blue Prism Login Agent 使用指导手册

    本文转自:https://cloud.tencent.com/developer/news/83035 咳!咳!咳! 第一篇RPA技术文,还是贼拉鸡冻.各位大侠要多多支持啊 1.Login Agent ...

  5. C# 设置程序启动项

    托盘图标设置 新建一个NotifyIcon,会在托盘处显示一个图标. NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标. notifyIcon.Icon = Syst ...

  6. 解决gitbook报错问题

    这个问题困扰了我 很久,网友给出了很多解决方案,我都亲测不靠谱. 以下解决方法亲测靠谱: OS:Win7 Gitbook版本: 3.2.3 Nodejs: V8.9.1 步骤: 1. 编辑文件 C:\ ...

  7. .net开源工作流ccflow从表数据数据源导入设置

    第1节. 关键字 驰骋工作流引擎 流程快速开发平台 workflow ccflow jflow  .net开源工作流 第2节. 从表数据导入设置 1.1.1: 概要说明 在从表的使用中我一般都会用到从 ...

  8. 【Android】用Cubism 2制作自己的Live2D——软件的安装与破解!

    前言- 上文我们简单的了解了Cubism的情况,但是Cubism 2.X安装好以后如果不进行破解只能使用Free版本,这是我们接受不了的,我们是专业的.是来学习的,怎么能不用Pro版本呢?所以话不多说 ...

  9. 转:Git Submodule管理项目子模块

    使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的submodule功能. 常用命令 git clone ...

  10. pyspider安装提示:got an unexpected keyword argument 'io_loop'的解决办法

    此问题解决办法学习自pyspider的github的issues 原地址:https://github.com/binux/pyspider/issues/771 解决方法: 由于最新版的Tornad ...