CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919
题目大意:斐波那契数列推导。给定前f1,f2,推出指定第N项。注意负数取模的方式:-1%(10^9+7)=10^9+6。
解题思路:
首先解出快速幂矩阵。以f3为例。 [f2] * [1 -1] = [f2-f1]=[f3] (幂1次)
[f1] * [1 0] [f2] [f2]
于是fn=[f2] *[1 -1]^(n-2)
[f1] [1 0]
注意一下负数取模。ans=(ans%mod+mod)%mod。
#include "cstdio"
#include "cstring"
#define LL long long
#define mod 1000000007
struct Matrix
{
LL mat[][];
Matrix() {memset(mat,,sizeof(mat));}
Matrix(int a,int b,int c,int d) {mat[][]=a;mat[][]=b;mat[][]=c;mat[][]=d;}
};
Matrix operator * (Matrix a,Matrix b)
{
Matrix ret;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
ret.mat[i][j]=;
for(int k=;k<;k++)
ret.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod;
}
return ret;
}
Matrix operator ^ (Matrix a,int n)
{
Matrix ret,base=a;
ret.mat[][]=ret.mat[][]=;
while(n)
{
if(n&) ret=ret*base;
base=base*base;
n>>=;
}
return ret;
}
int main()
{
LL a,b,n;
while(scanf("%I64d%I64d%I64d",&a,&b,&n)!=EOF)
{
if(n==) printf("%I64d\n",((a%mod)+mod)%mod);
else if(n==) printf("%I64d\n",((b%mod)+mod)%mod);
else
{
Matrix x(,-,,),tt;
tt=x^(n-);
LL ans=a*tt.mat[][]+b*tt.mat[][];
printf("%I64d\n",((ans%mod)+mod)%mod);
}
}
}
2824556 | neopenx | CodeForces 450B | Accepted | 0 KB | 78 ms | GNU C++ 4.6 | 1263 B | 2014-10-07 02:08:58 |
CodeForces 450B (矩阵快速幂模板题+负数取模)的更多相关文章
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- hdu 2604 矩阵快速幂模板题
/* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f( ...
- Final Destination II -- 矩阵快速幂模板题
求f[n]=f[n-1]+f[n-2]+f[n-3] 我们知道 f[n] f[n-1] f[n-2] f[n-1] f[n-2] f[n-3] 1 1 ...
- hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)
Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- hdu1575 Tr A 矩阵快速幂模板题
hdu1575 TrA 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 都不需要构造矩阵,矩阵是题目给的,直接套模板,把对角线上的数相加就好 ...
- 51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
- HDU1757又是一道矩阵快速幂模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 按照题目的要求构造矩阵 //Author: xiaowuga //矩阵: //a0 a1 a2 ...
随机推荐
- Coursera台大机器学习课程笔记8 -- Linear Regression
之前一直在讲机器为什么能够学习,从这节课开始讲一些基本的机器学习算法,也就是机器如何学习. 这节课讲的是线性回归,从使Ein最小化出发来,介绍了 Hat Matrix,要理解其中的几何意义.最后对比了 ...
- Aptana插件安装到eclipse和myeclipse的详细过程
刚开始学习Jquery,为了搭建好的环境是很重要的,所以我尝试了很多方式,下面之一. 一.要下载好Aptana 插件 官网: http://update1.aptana.org/studio/3.2/ ...
- 【SpringMVC】SpringMVC系列13之关于 mvc:annotation-driven
13.关于 mvc:annotation-driven 13.1.概述 会自动注册RequestMappingHandlerMapping.RequestMappingHandlerAdap ...
- Android PopupWindow 消失后的回掉方法
popupwindow 会在点击屏幕其他地方消失,但是这个消失时的回掉方法一直没有找到,困扰了一个多月,差了不少百度,没有很好解决,今天偶然看到了setOnDismissListener这个方法!没想 ...
- Delphi经验总结(3)
------------------------------------------------------- ◇删掉程序自己的exe文件 procedure TForm1.FormClose(Sen ...
- canvas API ,通俗的canvas基础知识(二)
上文我们讲到了画一条线,画矩形,写文字,总算是有了一个好的开头,如果还没有看的同学出门左转,先看看那篇,这里就不多做叙述了,接下来我们看比较复杂的一些属性和方法! 讲之前呢,我还是想温习一下,毕竟上文 ...
- Java for LeetCode 064 Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- git merge和个git rebase的区别
http://stackoverflow.com/questions/16666089/whats-the-difference-between-git-merge-and-git-rebase/16 ...
- Codeforces Round #321 (Div. 2)C(tree dfs)
题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...