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很大 这个问题和矩阵的特征值以 ...
随机推荐
- Josn转DataTable(转)
使用UI框架开发的时候就常常用到DataTable转Json的情况,但是最近完成一个微信公众号开发的项目,需要把微信接口传过来的json值作为转为DataTable后绑定到服务器控件上. 在网上找了很 ...
- iOS-tableView点击下拉菜单
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableVi ...
- hdu 1085 Holding Bin-Laden Captive!
Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...
- Ubuntu13.04手动安装nvidia显卡驱动
1. 下载最新版的nVidia驱动,命名为NVIDIA.run. http://www.nvidia.com/page/drivers.html 2.编辑blacklist.conf. sudo ge ...
- Linq的基础2
var 构建匿名类型1 = from c in ctx.GetTable<Customers>() select new ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- 2016_ThinkinG of everyDay
2016-2-15:新年的第一篇心情,慢慢更新. 1.以后的文章当然都会是ACM竞赛的题解,但会多写写自己的思考以及总结,因为感觉自己缺少思考这个习惯,有些东西在脑子里也只是一带而过,最后也不会停留多 ...
- The number of divisors(约数) about Humble Numbers
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- information_schema.triggers 学习
mysql实例中的每一个trigger 对应到information_schema.triggers 中有一行 1.information_schema.triggers 表的常用列: 1.trigg ...
- BZOJ 1064 假面舞会
http://www.lydsy.com/JudgeOnline/problem.php?id=1064 思路:第一眼看的时候以为是差分约束,但是是做不了的,不过能保证的就是这题绝对是图论题...(废 ...