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]^2)
能够求出关系矩阵
|1 0 0 0 |
A = |1 x^2 x 1 |
|0 2*x*y y 0 |
|0 y^2 0 0 |
这样就A了!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
const ll Mod = 10007;
const int N = 5;
int msize; struct Mat
{
ll mat[N][N];
}; Mat operator *(Mat a, Mat b)
{
Mat c;
memset(c.mat, 0, sizeof(c.mat));
for(int k = 0; k < msize; ++k)
for(int i = 0; i < msize; ++i)
if(a.mat[i][k])
for(int j = 0; j < msize; ++j)
if(b.mat[k][j])
c.mat[i][j] = (a.mat[i][k] * b.mat[k][j] + c.mat[i][j])%Mod;
return c;
} Mat operator ^(Mat a, ll k)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(int i = 0; i < msize; ++i)
c.mat[i][i]=1;
for(; k; k >>= 1)
{
if(k&1) c = c*a;
a = a*a;
}
return c;
} int main()
{
ll n,x,y;
msize = 4;
while(~scanf("%I64d%I64d%I64d",&n,&x,&y))
{
Mat A;
A.mat[0][0] = 1, A.mat[0][1] = 1, A.mat[0][2] = 0, A.mat[0][3] = 0;
A.mat[1][0] = 0, A.mat[1][1] = x*x%Mod, A.mat[1][2] = 2*x*y%Mod, A.mat[1][3] = y*y%Mod;
A.mat[2][0] = 0, A.mat[2][1] = x, A.mat[2][2] = y, A.mat[2][3] = 0;
A.mat[3][0] = 0, A.mat[3][1] = 1, A.mat[3][2] = 0, A.mat[3][3] = 0;
A = A^n;
printf("%I64d\n", (A.mat[0][0] + A.mat[0][1] + A.mat[0][2] + A.mat[0][3])%Mod);
}
return 0;
}
hdu 3306 Another kind of Fibonacci 矩阵快速幂的更多相关文章
- HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- HDU 5950:Recursive sequence(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...
- hdu 4565 So Easy! (共轭构造+矩阵快速幂)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...
随机推荐
- JNLP配置WIN10
依据网友的反响和我自己的考查,发觉JNLP的情况是大多数网友的困惑之处,针对此问题小编给大总结整理了(关键此)的处理技巧,并对它进以加工改善,整理出JNLP的详细教程,只用你按照1:首先配置电脑的ja ...
- Phoenix 使用教程
引言 hbase 提供很方便的 shell 脚本,可以对数据表进行 CURD 操作,但是毕竟是有一定的学习成本的,基本上对于开发来讲,sql 语句都是看家本领,那么,有没有一种方法可以把 sql 语句 ...
- 012.Kubernetes的configmap和secret配置
使用configmap对多有的配置文件进行统一管理 一 configmap配置管理 1.1 检查mysql的配置 [root@docker-server1 storage]# kubectl get ...
- rpm包名详解-rpm命令使用方法
linux软件包管理-rpm mount # 挂载 1.将光盘镜像插入光驱 2.创建挂载目录 mkdir /guangqu 3.挂载到/guangqu [root@gong ~]# mount /de ...
- 僵尸进程和孤儿进程-(转自Anker's Blog)
2.基本概念 我们知道在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程.子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束. 当 ...
- linux下 find命令使用
按名称查找 find . -name filename [root@vps repo]# ls README.md vps.sh[root@vps repo]# find . -iname vps ...
- USB NCM介绍
1 功能概述 USB NCM,属于USB-IF定义的CDC(Communication Device Class)下的一个子类:Network Control Model,用于Host和Device ...
- MyBatis执行器
Mybatis的执行器 下面先展示一张图,用来说明一下Mybatis执行器的整体架构 SimpleExecutor 首先SimpleExecutor是我们最常使用的一个执行器,无论我们执行什么方法默认 ...
- 解决idea查不到插件
http://127.0.0.1:1080
- Step By Step(Lua元表与元方法)
Step By Step(Lua元表与元方法) Lua中提供的元表是用于帮助Lua数据变量完成某些非预定义功能的个性化行为,如两个table的相加.假设a和b都是table,通过元表可以定义如何计算表 ...