CodeForces 450B Jzzhu and Sequences
矩阵快速幂。
首先得到公式
然后构造矩阵,用矩阵加速
取模函数需要自己写一下,是数论中的取模。
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<vector>
- #include<algorithm>
- using namespace std;
- long long MOD = 1e9 + ;
- long long x, y;
- int n;
- long long mod(long long a, long long b)
- {
- if (a >= ) return a%b;
- if (abs(a) % b == ) return ;
- return (a + b*(abs(a) / b + ));
- }
- struct Matrix
- {
- long long A[][];
- int R, C;
- Matrix operator*(Matrix b);
- };
- Matrix X, Y, Z;
- Matrix Matrix::operator*(Matrix b)
- {
- Matrix c;
- memset(c.A, , sizeof(c.A));
- int i, j, k;
- for (i = ; i <= R; i++)
- for (j = ; j <= C; j++)
- for (k = ; k <= C; k++)
- c.A[i][j] = mod((c.A[i][j] + mod(A[i][k] * b.A[k][j], MOD)), MOD);
- c.R=R; c.C=b.C;
- return c;
- }
- void read()
- {
- scanf("%lld%lld%d", &x, &y, &n);
- }
- void init()
- {
- n = n - ;
- Z.A[][] = x, Z.A[][] = y; Z.R = ; Z.C = ;
- Y.A[][] = , Y.A[][] = , Y.A[][] = , Y.A[][] = ; Y.R = ; Y.C = ;
- X.A[][] = , X.A[][] = -, X.A[][] = , X.A[][] = ; X.R = ; X.C = ;
- }
- void work()
- {
- while (n)
- {
- if (n % == ) Y = Y*X;
- n = n >> ;
- X = X*X;
- }
- Z = Z*Y;
- printf("%lld\n", mod(Z.A[][], MOD));
- }
- int main()
- {
- read();
- init();
- work();
- return ;
- }
CodeForces 450B Jzzhu and Sequences的更多相关文章
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- codeforces 450B. Jzzhu and Sequences 解题报告
题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...
- CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD
题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 ...
- 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 ...
随机推荐
- swool安装
swoole安装 安装完PHP后,即可安装swoole扩展. swoole扩展下载地址:https://github.com/swoole/swoole-src/releases 尽量选择stable ...
- OpenGL------显示列表
我们已经知道,使用OpenGL其实只要调用一系列的OpenGL函数就可以了.然而,这种方式在一些时候可能导致问题.比如某个画面中,使用了数千个多边形来表现一个比较真实的人物,OpenGL为了产生这数千 ...
- RING0,RING1,RING2,RING3
Intel的CPU将特权级别分为4个级别:RING0,RING1,RING2,RING3.Windows只使用其中的两个级别RING0和RING3,RING0只给操作系统用,RING3谁都能用.如果普 ...
- NPAPI简介
原文出处:http://blog.csdn.net/wwwsq/article/details/8254911 NPAPI是电脑上大部分非ie浏览器都支持的一种浏览器扩展,类似ie的activex技术 ...
- mysql 数字字段的类型选择
bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 ...
- My workbench draft
System Linux / Ubuntu 14.04.5 LTS (Trusty Tahr) + ROS Indigo Linux / Ubuntu 16.04.1 LTS (Xenial Xeru ...
- zendstudio的安装与配置
<微信公众平台应用开发实战>第1章搭建开发环境和相关技术介绍,本章会先介绍微信公众平台的一些基本概念和公众平台的开发模式:然后讲解如何搭建开发环境—AppServ和zendstudio:然 ...
- storm的并发
1 storm并行的基本概念 storm集群中的一个机器可以运行一个或者多个worker,对应于一个或者多个topologies. 1个worker进程运行1个或多个excutor线程.每个worke ...
- Eclipse中代码提示框的背景色修改
Preferences->General->Appearance->Colors and Fonts 修改Basic中的Content Assist backgroud color[ ...
- 转载 C++学习第9篇---类和类的封装
http://blog.csdn.net/zuheyawen/article/details/7324340