HDU 3306 - Another kind of Fibonacci
给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).
求 S(N) = A(0) 2 +A(1) 2+……+A(n) 2.
由于线性代数只能做线性变换,故要得出 A(n) 2 的递推式
A(n) 2 =X2A(N-1)2+Y2A(N-2)2+2XYA(N-1)A(N-2);
难点就在于 A(N-1)A(N-2) 这一项
故找到此项递推式
A(N-1)A(N-2) = (XA(N-2)+YA(N-3))*A(N-2) = XA(N-2)2+YA(N-2) A(N-3);
就可以写成矩阵
|1 X2 Y2 2XY| | S(n-1) | | S(n) |
|0 X2 Y2 2XY| * | A(n-1)2 | = | A(n)2 |
|0 0 1 0 | |A(n-2)2 | |A(n-1)2 |
|0 0 X Y | |A(n-2)A(n-3)| |A(n-1)A(n-2)|
剩下就是矩阵快速幂了
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct P{
int a[][];
}c,s,q;
int n,k,mod;
int a[][];
void ini()
{
memset(s.a,,sizeof(s.a));
for(int i=;i<=n;i++) s.a[i][i]=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
s.a[i][j+n]=s.a[i+n][j+n]=a[i][j];
memset(q.a,,sizeof(q.a));
for(int i=;i<=n;i++) q.a[i+n][i]=;
}
P mult(const P& a,const P& b)
{
P c;
for(int i=;i<=*n;++i)
{
for(int j=;j<=*n;++j)
{
c.a[i][j]=;
for(int k=;k<=*n;k++)
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j] )%mod;
}
}
return c;
}
void fuc(int p)
{
memset(c.a,,sizeof(c.a));
for(int i=;i<=n*;i++) c.a[i][i]=;
while(p)
{
if(p&) c=mult(c,s);
s=mult(s,s);
p>>=;
}
}
int main()
{
while(~scanf("%d%d%d",&n,&k,&mod))
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&a[i][j]);
a[i][j]%mod;
}
}
ini();
fuc(k);
P ans;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
ans.a[i][j]=;
for(int k=i;k<=*n;k++)
ans.a[i][j]=(ans.a[i][j]+c.a[i][k]*q.a[k][j] )%mod;
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++) printf("%d ",ans.a[i][j]);
puts("");
}
}
}
HDU 3306 - Another kind of Fibonacci的更多相关文章
- HDU 3306 Another kind of Fibonacci ---构造矩阵***
Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...
- hdu 3306 Another kind of Fibonacci(矩阵高速幂)
Another kind of Fibonacci Time Limit: 3000/10 ...
- HDU 3306 Another kind of Fibonacci(快速幂矩阵)
题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...
- hdu 3306 Another kind of Fibonacci 矩阵快速幂
参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...
- hdu 1250 Hat's Fibonacci
pid=1250">点击此处就可以传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding ...
- HDU 1708 简单dp问题 Fibonacci String
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu acmsteps 2.2.1 Fibonacci
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 5451 Best Solver(fibonacci)
感谢这道题让我复习了一遍线代,还学习了一些奇奇怪怪的数论. 令 二项展开以后根号部分抵消了 显然有 所以要求的答案是 如果n比较小的话,可以直接对二项式快速幂,但是这题n很大 这个问题和矩阵的特征值以 ...
随机推荐
- VS2015预览版中的C#6.0 新功能(二)
VS2015预览版中的C#6.0 新功能(一) VS2015预览版中的C#6.0 新功能(三) 自动属性的增强 只读自动属性 以前自动属性必须同时提供setter和getter方法,因而只读属性只能通 ...
- Ubuntu14.10下解决chromium浏览器无法安装adobe flash的问题
本文参考了一下资源和博客,在此提出表示感谢: http://my.oschina.net/u/209016/blog/290067 http://ubuntuhandbook.org/index.ph ...
- gcc 编译的4个过程简单识记
直入正题,测试编译代码如下: lude <stdio.h> int main() { ,y,z; x*=(y=z=); printf("%d\n",x); z=; x= ...
- (原+转)ubuntu16中莫名死机及重新安装显卡驱动
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5992693.html 参考网址: http://blog.csdn.net/u012581999/ar ...
- 子元素用margin-top 为什么反而作用在父元素上?对使用margin-top 的元素本身不起作用?
在这个说明中,“collapsing margins”(折叠margin)的意思是:2个或以上盒模型之间(关系可以是相邻或嵌套)相邻的margin属性(这之间不能有非空内容.padding区域.bor ...
- 使用jekyll和prose在github上创建博客
利用github的pages服务可以很方便地显示和管理我们的静态页面,这样用来做博客是非常适合的. 1.首先你要有一个github的帐号 2.创建一个repo,名字叫username.github.i ...
- [转]Windows平台下LispBox环境搭建
转自http://www.cnblogs.com/sunt2012/archive/2013/05/27/3102816.html Lisp in a Box软件包可以让新Lisp程序员在一流的Lis ...
- C# sql Helper
using System; using System.Collections; using System.Collections.Generic; using System.Configuration ...
- hdu4393 Throw nails(只用模拟前面500来次,后面根据速度、位置、id值排序即可)
...
- Android中调用C++函数的一个简单Demo
这里我不想多解释什么,对于什么JNI和NDK的相关内容大家自己去百度或谷歌.我对Android的学习也只是个新手.废话少说直接进入正题. 一.在Eclipse中创建一个Android Applicat ...