[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 ...
随机推荐
- codevs1026商务旅行
1036 商务旅行 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 某首都城市的商人要经常到各城镇去做 ...
- 343 Integer Break 整数拆分
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积.例如,给定 n = 2,返回1(2 = 1 + 1):给定 n = 10,返回36(10 = 3 ...
- [转]Linux定时任务Crontab详解
转自:http://blog.chinaunix.net/uid-7552018-id-182133.html 今天做了个数据库的备份脚本,顺便系统得学习一下Linux下定时执行脚本的设置.Linux ...
- JVM 优化之逃逸分析
整理自 周志明<深入JVM> 1, 是JVM优化技术,它不是直接优化手段,而是为其它优化手段提供依据. 2,逃逸分析主要就是分析对象的动态作用域. 3,逃逸有两种:方法逃逸和线程逃逸. ...
- Django用户认证系统
一. 认证系统概要 create_user 创建用户 authenticate 验证登录 login 记住用户的登录状态 logout 退出登录 is_authenticated 判断用户是否登录 l ...
- 使用Hexo搭建个人博客配置全过程
大致过程分为: 1.搭建Node.js 环境 2. 搭建Git 环境 3.安装配置Hexo 4.GitHub 注册和配置 5. 关联Hexo 与 GitHub Pages 7.Hexo的常用操作 下面 ...
- ProE常用曲线方程:Python Matplotlib 版本代码(玫瑰曲线)
Pyplot教程:https://matplotlib.org/gallery/index.html#pyplots-examples 玫瑰曲线 文字描述 平面内,围绕某一中心点平均分布整数个正弦花瓣 ...
- 论文deadline 最后三天
2015.12.29 星期二 内容整改 2015.12.30 星期三 参考文献,摘要等 2015.12.31 星期四 最后修改 尽最大的努力去做好论文的事情.
- HDU_1028_Ignatius and the Princess III_(母函数,dp)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- jenkins配置发送测试结果邮件
简单版: https://www.cnblogs.com/gcgc/p/5631385.html https://blog.51cto.com/qicheng0211/1919341 升级版 http ...