基础题。。

wa在n的范围需要用long long

= =、长个记性

#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const LL mod=1e9+9; struct asd{
LL a[2][2];
}; asd mul(asd x,asd y)
{
asd ans;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
ans.a[i][j]=0;
for(int k=0;k<2;k++)
{
ans.a[i][j]=ans.a[i][j]+(x.a[i][k]*y.a[k][j])%mod;
ans.a[i][j]%=mod;
}
}
}
return ans;
}
asd quickmul(LL g,asd x)
{
asd ans;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
if(i==j) ans.a[i][j]=1;
else ans.a[i][j]=0;
}
}
while(g)
{
if(g&1) ans=mul(ans,x);
x=mul(x,x);
g>>=1;
}
return ans;
} int main()
{
LL n;
scanf("%lld",&n);
if(n==0||n==1)
{
printf("%lld\n",n);
return 0;
}
asd x,ans;
x.a[0][0]=1;x.a[0][1]=1;
x.a[1][0]=1;x.a[1][1]=0;
ans=quickmul(n-1,x);
printf("%lld\n",ans.a[0][0]);
return 0;
}

51nod1242【矩阵快速幂】的更多相关文章

  1. 矩阵快速幂--51nod-1242斐波那契数列的第N项

    斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...

  2. 51nod1242斐波那契数列的第N项 【矩阵快速幂】

    斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, 34, ...

  3. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  4. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  5. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  8. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  9. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  10. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

随机推荐

  1. Python 元祖、列表、字典、文件(转载)

    转自http://yangsq.iteye.com/category/20857 python的元组.列表.字典数据类型是很python(there python is a adjective)的数据 ...

  2. XShell连接不了虚拟机

    本机安装好虚拟机和centeros; 使用xshell连接: linux Could not connect to '127.0.0.1' (port 22): Connection failed. ...

  3. Javascript的参数详解

    函数可以有参数也可以没有参数,如果定义了参数,在调用函数的时候没有传值,默认设置为undefined 在调用函数时如果传递参数超过了定义时参数,jS会忽略掉多余参数 jS中不能直接写默认值,可以通过a ...

  4. Boost 库编译总结

    1. 下载boost库源码,解压缩. 2. 打开vs2010 工具栏tools 下的visual studio command prompt,运行源码目录下的bootstrap.bat,生成bjam. ...

  5. Raspberry Pi3 ~ 安装 nfs Server

    l  安装必要服务: sudo      apt-get install  portmap sudo  apt-get install  nfs-kernel-server sudo      apt ...

  6. netstat 参数

    Netstat用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况. NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] ...

  7. Android源码的编译和下载【转】

    本文转载自:http://blog.csdn.net/banketree/article/details/9089827 网上介绍下载.编译Android方法一坨,读万卷书不如行万里路,以下是笔者亲身 ...

  8. Java生成UUID不重复的id值

    在Java中创建UUID在网上查资料才知道在Java中,变成了UUID.创建方式也出奇简单System.out.println( java.util.UUID.randomUUID());

  9. uva 10881 Piotr's Ants 解题报告

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pa ...

  10. codeforces A. Fox and Box Accumulation 解题报告

    题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...