HDU 1005 Number Sequence:矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005
题意:
数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n-1) + B*f(n-2) ) MOD 7
给定A、B、n,求f(n)。 (1<=n<=100,000,000)
题解:
大水题~ (*/ω\*)
矩阵快速幂。
初始矩阵start:

特殊矩阵special:

所求矩阵ans:
ans = start * special^(n-1)
ans的第一项即为f(n)。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_L 5
#define MOD 7 using namespace std; struct Mat
{
int n;
int m;
int val[MAX_L][MAX_L];
Mat()
{
n=;
m=;
memset(val,,sizeof(val));
}
}; int a,b,n; Mat make_unit(int n)
{
Mat mat;
mat.n=n;
mat.m=n;
for(int i=;i<n;i++)
{
mat.val[i][i]=;
}
return mat;
} Mat make_start()
{
Mat mat;
mat.n=;
mat.m=;
mat.val[][]=;
mat.val[][]=;
return mat;
} Mat make_special()
{
Mat mat;
mat.n=;
mat.m=;
mat.val[][]=;
mat.val[][]=b;
mat.val[][]=;
mat.val[][]=a;
return mat;
} Mat mul_mat(const Mat &a,const Mat &b)
{
Mat c;
if(a.m!=b.n)
{
cout<<"Error: mat_mul"<<endl;
return c;
}
c.n=a.n;
c.m=a.m;
for(int i=;i<a.n;i++)
{
for(int j=;j<b.m;j++)
{
for(int k=;k<a.m;k++)
{
c.val[i][j]+=a.val[i][k]*b.val[k][j];
c.val[i][j]%=MOD;
}
}
}
return c;
} Mat quick_pow_mat(Mat mat,long long k)
{
Mat ans;
if(mat.n!=mat.m)
{
cout<<"Error: quick_pow_mat"<<endl;
return ans;
}
ans=make_unit(mat.n);
while(k)
{
if(k&)
{
ans=mul_mat(ans,mat);
}
mat=mul_mat(mat,mat);
k>>=;
}
return ans;
} int main()
{
while(cin>>a>>b>>n)
{
if(a== && b== && n==) break;
Mat start=make_start();
Mat special=make_special();
Mat ans=mul_mat(start,quick_pow_mat(special,n-));
cout<<ans.val[][]<<endl;
}
}
HDU 1005 Number Sequence:矩阵快速幂的更多相关文章
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- Yet another Number Sequence 矩阵快速幂
Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...
- SDUT1607:Number Sequence(矩阵快速幂)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
随机推荐
- Shiro学习(7)与Web整合
Shiro提供了与Web集成的支持,其通过一个ShiroFilter入口来拦截须要安全控制的URL.然后进行对应的控制,ShiroFilter相似于如Strut2/SpringMVC这样的web框架的 ...
- Java Transaction Management
Just a few weeks ago, I had a discussion with one of my colleagues about how to manage the transacti ...
- 重读金典------高质量C编程指南(林锐)-------第三章 命名规则
3.1 共性规则 规则:标识符应该直观且可以拼读,可进行英语翻译. 规则:标识符的长度需要控制好,不应该太长. 规则:命名规则应该同操作系统或者开发工具等保持一致,比如大小写混用.AddChar ...
- iOS陆哥开发笔记(七) (AVFoundation简单介绍)
在AVFoundation框架中AVAudioRecorder类专门处理录音操作,支持多种音频格式. 以下是经常使用的属性和方法: 属性 说明 @property(readonly, getter=i ...
- linux - console/terminal/virtual console/pseudo terminal ...
http://en.wikipedia.org/wiki/System_console System console Knoppix system console showing the boot p ...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- linux配置nfs步骤及心得
这节我们介绍NFS的相关概念,以及怎样配置NFS和在client中查看NFS. NFS的配置过程非常easy. 在server端中编辑/etc/exports文件,加入例如以下内容: ...
- ui-router $transitions 用法
1. //route redirection $transitions.onStart({to: 'manage'}, function (trans) { var params = trans.pa ...
- JavaWeb学习总结第一篇--初识JavaWeb
JavaWeb学习总结(一)-- 初识JavaWeb 一:Web相关概念 Web程序也就是一般所说的网站,由服务器.客户端浏览器和网络组成.Web程序的好处就是使用简单,不需要安装.学习,有一台电脑. ...
- SRM 626 D1L1: FixedDiceGameDiv1,贝叶斯公式,dp
题目:http://community.topcoder.com/stat?c=problem_statement&pm=13239&rd=15859 用到了概率论中的贝叶斯公式,而贝 ...