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 ...
随机推荐
- PHP执行linux系统命令
本文是第一篇,讲述如何在PHP中执行系统命令从而实现一些特殊的目的,比如监控服务器负载,重启MySQL.更新SVN.重启Apache等.第二篇<PHP监控linux服务器负载>:http: ...
- HDU5294——Tricks Device(最短路 + 最大流)
第一次做最大流的题目- 这题就是堆模板 #include <iostream> #include <algorithm> #include <cmath> #inc ...
- android Gallery2 onPause时候,其背景界面显示黑色
改动: Src/com/android/gallery3d/app/AbstracGalleryActivity.java OnResume()函数约290行 去掉 mGLRootView.setVi ...
- ssh登录慢的解决办法
ubuntu的ssh登录有点慢,其实是很慢 google了一把,发现可以这样解决: (1)可能是DNS反向解析的问题 对于这样的问题,可以在/etc/ssh/sshd_config 中添加/修改: U ...
- win10系统架构调用
操作系统模型 操作系统有两种模式: 用户模式 内核模式 当用户模式调用系统服务时,CPU执行一个特殊的指令以切换到内核模式(Ring0),当系统服务调用完成时,操作系统切换回用户模式(Ring3). ...
- PythonCookBook笔记——函数
函数 可接受任意数量参数的函数 接受任意数量的位置参数,使用*参数. 接受任意数量的关键字参数,使用**参数. 只接受关键字参数的函数 强制关键字参数放在某个参数后或直接单个之后. 给函数参数增加元信 ...
- c 字符串 函数
c编辑 strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NUL结 ...
- iOS 键盘变中文
plist文件添加 Localizations 添加一项字段Chinese (simplified)
- KVC基本使用
首先,创建两个类.person类和book类.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/ ...
- SRM 626 D1L1: FixedDiceGameDiv1,贝叶斯公式,dp
题目:http://community.topcoder.com/stat?c=problem_statement&pm=13239&rd=15859 用到了概率论中的贝叶斯公式,而贝 ...