洛谷 P1962 斐波那契数列
题目链接:https://www.luogu.org/problemnew/show/P1962
题目大意:
略
分析:
代码如下:
- #include <bits/stdc++.h>
- using namespace std;
- #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
- #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
- #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
- #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
- #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
- #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
- #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
- #define pr(x) cout << #x << " = " << x << " "
- #define prln(x) cout << #x << " = " << x << endl
- #define LOWBIT(x) ((x)&(-x))
- #define ALL(x) x.begin(),x.end()
- #define INS(x) inserter(x,x.begin())
- #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
- #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
- #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
- #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
- #define ms0(a) memset(a,0,sizeof(a))
- #define msI(a) memset(a,0x3f,sizeof(a))
- #define msM(a) memset(a,-1,sizeof(a))
- #define MP make_pair
- #define PB push_back
- #define ft first
- #define sd second
- template<typename T1, typename T2>
- istream &operator>>(istream &in, pair<T1, T2> &p) {
- in >> p.first >> p.second;
- return in;
- }
- template<typename T>
- istream &operator>>(istream &in, vector<T> &v) {
- for (auto &x: v)
- in >> x;
- return in;
- }
- template<typename T>
- ostream &operator<<(ostream &out, vector<T> &v) {
- Rep(i, v.size()) out << v[i] << " \n"[i == v.size() - ];
- return out;
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
- out << "[" << p.first << ", " << p.second << "]" << "\n";
- return out;
- }
- inline int gc(){
- static const int BUF = 1e7;
- static char buf[BUF], *bg = buf + BUF, *ed = bg;
- if(bg == ed) fread(bg = buf, , BUF, stdin);
- return *bg++;
- }
- inline int ri(){
- int x = , f = , c = gc();
- for(; c<||c>; f = c=='-'?-:f, c=gc());
- for(; c>&&c<; x = x* + c - , c=gc());
- return x*f;
- }
- template<class T>
- inline string toString(T x) {
- ostringstream sout;
- sout << x;
- return sout.str();
- }
- inline int toInt(string s) {
- int v;
- istringstream sin(s);
- sin >> v;
- return v;
- }
- //min <= aim <= max
- template<typename T>
- inline bool BETWEEN(const T aim, const T min, const T max) {
- return min <= aim && aim <= max;
- }
- typedef unsigned int uI;
- typedef long long LL;
- typedef unsigned long long uLL;
- typedef vector< int > VI;
- typedef vector< bool > VB;
- typedef vector< char > VC;
- typedef vector< double > VD;
- typedef vector< string > VS;
- typedef vector< LL > VL;
- typedef vector< VI > VVI;
- typedef vector< VB > VVB;
- typedef vector< VS > VVS;
- typedef vector< VL > VVL;
- typedef vector< VVI > VVVI;
- typedef vector< VVL > VVVL;
- typedef pair< int, int > PII;
- typedef pair< LL, LL > PLL;
- typedef pair< int, string > PIS;
- typedef pair< string, int > PSI;
- typedef pair< string, string > PSS;
- typedef pair< double, double > PDD;
- typedef vector< PII > VPII;
- typedef vector< PLL > VPLL;
- typedef vector< VPII > VVPII;
- typedef vector< VPLL > VVPLL;
- typedef vector< VS > VVS;
- typedef map< int, int > MII;
- typedef unordered_map< int, int > uMII;
- typedef map< LL, LL > MLL;
- typedef map< string, int > MSI;
- typedef map< int, string > MIS;
- typedef set< int > SI;
- typedef stack< int > SKI;
- typedef deque< int > DQI;
- typedef queue< int > QI;
- typedef priority_queue< int > PQIMax;
- typedef priority_queue< int, VI, greater< int > > PQIMin;
- const double EPS = 1e-;
- const LL inf = 0x7fffffff;
- const LL infLL = 0x7fffffffffffffffLL;
- const LL mod = 1e9 + ;
- const int maxN = 1e6 + ;
- const LL ONE = ;
- const LL evenBits = 0xaaaaaaaaaaaaaaaa;
- const LL oddBits = 0x5555555555555555;
- struct Matrix{
- int row, col;
- LL MOD;
- VVL mat;
- Matrix(int r, int c, LL p = mod) : row(r), col(c), MOD(p) {
- mat.assign(r, VL(c, ));
- }
- Matrix(const Matrix &x, LL p = mod) : MOD(p){
- mat = x.mat;
- row = x.row;
- col = x.col;
- }
- Matrix(const VVL &A, LL p = mod) : MOD(p){
- mat = A;
- row = A.size();
- col = A[].size();
- }
- // x * 单位阵
- inline void E(int x = ) {
- assert(row == col);
- Rep(i, row) mat[i][i] = x;
- }
- inline VL& operator[] (int x) {
- assert(x >= && x < row);
- return mat[x];
- }
- inline Matrix operator= (const VVL &x) {
- row = x.size();
- col = x[].size();
- mat = x;
- return *this;
- }
- inline Matrix operator+ (const Matrix &x) {
- assert(row == x.row && col == x.col);
- Matrix ret(row, col);
- Rep(i, row) {
- Rep(j, col) {
- ret.mat[i][j] = mat[i][j] + x.mat[i][j];
- ret.mat[i][j] %= MOD;
- }
- }
- return ret;
- }
- inline Matrix operator* (const Matrix &x) {
- assert(col == x.row);
- Matrix ret(row, x.col);
- Rep(k, x.col) {
- Rep(i, row) {
- if(mat[i][k] == ) continue;
- Rep(j, x.col) {
- ret.mat[i][j] += mat[i][k] * x.mat[k][j];
- ret.mat[i][j] %= MOD;
- }
- }
- }
- return ret;
- }
- inline Matrix operator*= (const Matrix &x) { return *this = *this * x; }
- inline Matrix operator+= (const Matrix &x) { return *this = *this + x; }
- };
- // 矩阵快速幂,计算x^y
- inline Matrix mat_pow_mod(Matrix x, LL y) {
- Matrix ret(x.row, x.col);
- ret.E();
- while(y){
- if(y & ) ret *= x;
- x *= x;
- y >>= ;
- }
- return ret;
- }
- LL N;
- Matrix ans(VVL({{, }, {, }}));
- int main(){
- //freopen("MyOutput.txt","w",stdout);
- //freopen("input.txt","r",stdin);
- //INIT();
- scanf("%lld", &N);
- ans = mat_pow_mod(ans, N);
- printf("%lld\n", ans.mat[][]);
- return ;
- }
洛谷 P1962 斐波那契数列的更多相关文章
- 洛谷P1962 斐波那契数列【矩阵运算】
洛谷P1962 斐波那契数列[矩阵运算] 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) ( ...
- 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]
P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...
- 洛谷——P1962 斐波那契数列
P1962 斐波那契数列 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 ...
- 洛谷—— P1962 斐波那契数列
https://www.luogu.org/problem/show?pid=1962 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- 洛谷P1962 斐波那契数列题解
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- 【洛谷P1962 斐波那契数列】矩阵快速幂+数学推导
来提供两个正确的做法: 斐波那契数列双倍项的做法(附加证明) 矩阵快速幂 一.双倍项做法 在偶然之中,在百度中翻到了有关于斐波那契数列的词条(传送门),那么我们可以发现一个这个规律$ \frac{F_ ...
- 题解——洛谷P1962 斐波那契数列(矩阵乘法)
矩阵乘法加速线性递推的典型 大概套路就是先构造一个矩阵\( F \)使得另一初始矩阵\( A \)乘以\( F^{x} \)能够得出第n项 跑的飞快 虽然我也不知道那个矩阵要怎么构造 或许就像我使用了 ...
- 洛谷P1962 斐波那契数列
传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...
随机推荐
- java~springboot~ibatis数组in查询的实现
在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如where in这种从数组里查询符合条件的集合里,需要在xml ...
- MySQL系列--3.数据类型和连接查询
1.存储引擎 数据创建,查询,更新和删除操作都是通过数据引擎来进行的.不同的存储引擎存储限制不同,支持不同的索引机制等. 查询数据库支持的存储引擎 MySQL 5.7.2支持的存储引擎有:InnoDB ...
- spring boot 集成 zookeeper 搭建微服务架构
PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...
- [转]Blue Prism Login Agent 使用指导手册
本文转自:https://cloud.tencent.com/developer/news/83035 咳!咳!咳! 第一篇RPA技术文,还是贼拉鸡冻.各位大侠要多多支持啊 1.Login Agent ...
- C# 设置程序启动项
托盘图标设置 新建一个NotifyIcon,会在托盘处显示一个图标. NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标. notifyIcon.Icon = Syst ...
- 解决gitbook报错问题
这个问题困扰了我 很久,网友给出了很多解决方案,我都亲测不靠谱. 以下解决方法亲测靠谱: OS:Win7 Gitbook版本: 3.2.3 Nodejs: V8.9.1 步骤: 1. 编辑文件 C:\ ...
- .net开源工作流ccflow从表数据数据源导入设置
第1节. 关键字 驰骋工作流引擎 流程快速开发平台 workflow ccflow jflow .net开源工作流 第2节. 从表数据导入设置 1.1.1: 概要说明 在从表的使用中我一般都会用到从 ...
- 【Android】用Cubism 2制作自己的Live2D——软件的安装与破解!
前言- 上文我们简单的了解了Cubism的情况,但是Cubism 2.X安装好以后如果不进行破解只能使用Free版本,这是我们接受不了的,我们是专业的.是来学习的,怎么能不用Pro版本呢?所以话不多说 ...
- 转:Git Submodule管理项目子模块
使用场景 当项目越来越庞大之后,不可避免的要拆分成多个子模块,我们希望各个子模块有独立的版本管理,并且由专门的人去维护,这时候我们就要用到git的submodule功能. 常用命令 git clone ...
- pyspider安装提示:got an unexpected keyword argument 'io_loop'的解决办法
此问题解决办法学习自pyspider的github的issues 原地址:https://github.com/binux/pyspider/issues/771 解决方法: 由于最新版的Tornad ...