2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)
题意

Sol
直接矩阵快速幂
推出来的矩阵应该长这样
\begin{equation*}
\begin{bmatrix}
1&1&1&1&1&1\\
1 & 0&0&0&0&0\\
0 & 0&1&3&3&1\\
0 & 0&0&1&2&1\\
0 & 0&0&0&1&1\\
0 & 0&0&0&0&1\\
\end{bmatrix}^{i - 1}*
\begin{bmatrix}
F_{1}\\
F_0\\
1\\
1\\
1\\
1
\end{bmatrix}=
\begin{bmatrix}
1&1&1&1&1&1\\
1 & 0&0&0&0&0\\
0 & 0&1&3&3&1\\
0 & 0&0&1&2&1\\
0 & 0&0&0&1&1\\
0 & 0&0&0&0&1\\
\end{bmatrix}*
\begin{bmatrix}
F_{i - 1}\\
F_{i - 2}\\
i^3\\
i^2\\
i\\
1
\end{bmatrix}=
\begin{bmatrix}
F_{i}\\
F_{i - 1}\\
(i + 1)^3\\
(i + 1)^2\\
i + 1\\
1
\end{bmatrix}
\end{equation*}
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long
//#define int long long
using namespace std;
const int mod = 1e9 + ;
inline LL read() {
char c = getchar(); LL x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int T;
LL N;
struct Matrix {
LL a[][], N;
Matrix() {
N = ;
memset(a, , sizeof(a));
}
Matrix operator * (const Matrix &rhs) const {
Matrix ans;
for(int k = ; k <= N; k++)
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
(ans.a[i][j] += (1ll * a[i][k] * rhs.a[k][j]) % mod) %= mod;
return ans;
}
};
Matrix fp(Matrix a, LL p) {
Matrix base;
// printf("%d", base.a[0][1]);
for(int i = ; i <= ; i++) base.a[i][i] = ;
while(p) {
if(p & ) base = base * a;
a = a * a; p >>= ;
}
return base;
}
const LL GG[][] = {
{, , , , , , },
{, , , , , , },
{, , , , , , },
{, , , , , , },
{, , , , , , },
{, , , , , , },
{, , , , , , }
};
int main() {
T = read();
while(T--) {
N = read();
if(N == ) {puts(""); continue;}
if(N == ) {puts(""); continue;}
Matrix M;
memcpy(M.a, GG, sizeof(M.a));
Matrix ans = fp(M, N - );
LL out = ;
(out += ans.a[][] * ) %= mod;
(out += ans.a[][] * ) %= mod;
(out += ans.a[][] * ) %= mod;
(out += ans.a[][] * ) %= mod;
(out += ans.a[][] * ) %= mod;
(out += ans.a[][]) %= mod;
printf("%lld\n", out % mod);
}
return ;
}
/*
5
4
1
2
3
100
*/
2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)的更多相关文章
- 2018年湘潭大学程序设计竞赛G又见斐波那契
链接:https://www.nowcoder.com/acm/contest/105/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 2018年湘潭大学程序设计竞赛 G- 又见斐波那契
推一推矩阵直接快速幂. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> #defi ...
- 牛客网-2018年湘潭大学程序设计竞赛-F
题目链接:https://www.nowcoder.com/acm/contest/105/F 解题思路:这道题第一眼直接思路就是搜索,但想了半天没想到有什么好办法搜,然后就转成最短路写了, 因为多入 ...
- 2018年湘潭大学程序设计竞赛 H统计颜色
链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 2018年湘潭大学程序设计竞赛 F - maze
把点抽出来 跑个最短路就好啦. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> # ...
- 2018年湘潭大学程序设计竞赛 E 吃货
题目描述 作为一个标准的吃货,mostshy又打算去联建商业街觅食了.混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种 ...
- 2018年湘潭大学程序设计竞赛 Fibonacci进制
Fibonacci数是非常有名的一个数列,它的公式为 f(n)=f(n-1)+f(n-2),f(0)=1,f(1)=2. 我们可以把任意一个数x表示成若干不相同的Fibonacci数的和, 比如说1 ...
- 2018年湘潭大学程序设计竞赛 maze(bfs)
链接:https://www.nowcoder.com/acm/contest/105/F来源:牛客网 有q个单向传送阵,每个传送阵各有一个入口和一个出口,入口和出口都在迷宫的格子里,当走到或被传送到 ...
随机推荐
- AIX 7.1上安装Oracle11g
1. 上传oracle 11g介质到AIX 我下载的介质是aix.ppc64_11gR2_database_1of2.zip和aix.ppc64_11gR2_database_2of2.zip, 执行 ...
- BZOJ3127:[USACO2013OPEN]Yin and Yang
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- 导出数据到EXCEL并生成多个Sheet
一.准备工作 引用:Microsoft.Office.Interop.Excel 准备多个DataTable数据添加到DataSet中. 二.代码 public void CreateExcel(Da ...
- python-pprint打印函数
#!/usr/bin/env python # -*- coding:utf-8 -*- import sys,pprint pprint.pprint(sys.path)
- HDU-5974
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 1、scala安装和基本语法
一.安装Scala 1.安装 因为Scala是基于Java虚拟机,也就是JVM的一门编程语言. 所有Scala的代码,都需要经过编译为字节码,然后交由Java虚拟机来运行. 所以Scala和Java是 ...
- C#生成满足特定要求的密码
代码1 Random m_rnd = new Random(); public char getRandomChar() { ); || (ret > && ret < ) ...
- Working Experience - WPF 中 DataGrid 控件的应用
问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...
- Razor与HTML混合输出陷阱与技巧
3,Razor与HTML混合输出陷阱与技巧 属性名称误判 有时候我们必须让html内容与razor语句紧接在一起, eg: 您好,a先生 假设变量名称为ViewBag.Name, 您好, ...
- Whatweb网站指纹信息收集工具
常规扫描:whatweb www.baidu.com 批量扫描: whatweb -i /root/12.txt 详细回显扫描:whatweb -v www.baidu.com 加强扫描强度:what ...