[poj3070]Fibonacci_矩乘_快速幂
Fibonacci poj-3070
题目大意:求Fibonacci第n项。
注释:模数为10000,$1\le n \le 10^9$。
想法:矩阵题,用例题6的想法,我们构造矩阵
$\begin{pmatrix} 0 & 1 \\ 1 & 1 \end{pmatrix}$
然后,我们快速幂即可。
附上lijinnn的版子
struct Matr
{
int a[4][4];
Matr(){memset(a,0,sizeof a);}
Matr operator *(const Matr &z)
{
Matr re;
int p=2;
for(int i=0;i<p;++i)
{
for(int j=0;j<p;++j)
{
for(int k=0;k<p;++k)
{
re.a[i][j]=(re.a[i][j]+a[i][k]*z.a[k][j])%mod;
}
}
}
return re;
}
};
这是一种重载运算符的方式,我开始用的是函数。但是在快速幂的时候确实有些慢(如果直接传参的话)。感谢lijinnn(吉林队长)。
其中,矩阵从零开始的空间优化特别多!此题没有...因为都没怎么开矩阵
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define mod 10000
using namespace std;
struct Matr
{
int a[3][3];
Matr(){memset(a,0,sizeof a);}
Matr operator *(const Matr &z)//重定义运算符
{
Matr re;
int p=2;
for(int i=0;i<p;++i)
{
for(int j=0;j<p;++j)
{
for(int k=0;k<p;++k)
{
re.a[i][j]=(re.a[i][j]+a[i][k]*z.a[k][j])%mod;
}
}
}
return re;
}
};
Matr quick_power(int x)//快速幂
{
Matr ori;
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
ori.a[i][i]=1;
}
}
Matr k;
k.a[0][1]=k.a[1][0]=k.a[1][1]=1;
while(x)
{
if(x&1) ori=ori*k;
k=k*k;
x>>=1;
}
return ori;
}
int main()
{
int n;
while(1)
{
scanf("%d",&n);
if(n==-1) return 0;
Matr s;
s=quick_power(n);
Matr ans;
ans.a[0][0]=-1;
ans=ans*s;
printf("%d\n",-ans.a[0][1]);//为了方便,我的初始矩阵用的是负数为的是如果求第i项,快速幂i次即可
//但是由于开始是负数,所以需要在最后乘上-1
}
}
小结:矩阵好东西,推荐自己的blog 矩阵算法学习摘记
[poj3070]Fibonacci_矩乘_快速幂的更多相关文章
- BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS
BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p, ...
- Luogu P1226 取余运算||快速幂_快速幂
超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...
- codeforces_300C_组合数_快速幂
C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- [bzoj2467][中山市选2010]生成树_快速幂
生成树 bzoj-2467 中山市选2010 题目大意:题目链接 注释:略. 想法:首先,考虑生成树的性质.每两个点之间有且只有一条路径.我们将每个五边形的5条边分为外面的4条边和内部的一条边,在此简 ...
- [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂
Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...
- [国家集训队]拉拉队排练 Manancher_前缀和_快速幂
Code: #include <cstdio> #include <algorithm> #include <cstring> using namespace st ...
- 矩阵快速幂——POJ3070
矩阵快速幂和普通的快速幂差不多,只不过写起来比较麻烦一点,需要重载*运算符. 模板: struct mat { int m[maxn][maxn]; }unit; mat operator * (ma ...
- nyoj1000_快速幂_费马小定理
又见斐波那契数列 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 斐波那契数列大家应该很熟悉了吧.下面给大家引入一种新的斐波那契数列:M斐波那契数列. M斐波那契数列 ...
- nyoj_148_fibonacci数列(二)_矩阵快速幂
fibonacci数列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the Fibonacci integer sequence, F0 = 0, F ...
随机推荐
- 使用display:flex;实现垂直水平居中
body,div{margin:0px;padding:0px;} .flex-container{display:flex;height:300px;background-color:#ddd;ju ...
- ural 1017. Staircases(dp)
http://acm.timus.ru/problem.aspx?space=1&num=1017 题意:有n块砖,要求按照严格递增的个数摆放成楼梯,求楼梯的摆放种类数. 思路:状态转移方程: ...
- Graphics.DrawMeshInstanced
Draw the same mesh multiple times using GPU instancing. 可以免去创建和管理gameObj的开销 并不是立即绘制,如需:Graphics.Draw ...
- 你真的知道GET和POST两种基本请求方法的区别吗?
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- Android数据存储的5种方法
--使用SharedPreferences存储数据 --文件存储数据 --SQLite数据库存储数据 --使用ContentProvider存储数据 --网络存储数据 Preference,File, ...
- ansible基础知识
安装ansible epel源 第一步: 下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel- ...
- Microsoft SQL Server 2008/2012 Internals 一处疑问
Kalen Delaney 等著的深入解析 Microsoft SQL Server 系列,享有盛誉,深入研读,是管窥深奥复杂之 SQL Server 的阶梯与门径.手头有 Microsoft SQL ...
- oracle数据库忘记用户名和密码莫着急
刚安装完Oracle 11g后,登录的时候没有记住用户名和密码,解决方法:新建一个用户 第一步:以系统身份登录 cmd--->sqlplus 提示输入用户名,然后输入sqlplus/as sys ...
- linux 安装 mongo
整个安装过程:下载安装包--> 解压,添加系统路径(是滴,不用安装解压即可) --> 创建数据目录 --> 启动mongod服务 --> 运行mongo 1.下载安装包 Mon ...
- HTML CSS, JavaScript 计算器
效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...