武大邀请赛的网络预选赛,就去做了个签到题,居然连这个递推都没推出来,真是惭愧。

而且好久没写矩阵乘法了,来回顾一下。

题意:

  求Fibonacci数列的,前n项立方和。

思路:

  可以求得一下递推公式:

然后用矩阵快速幂求出结果即可。

代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional> using namespace std; typedef long long ll; const ll MOD = (ll)1e9+;
const ll ONE[][] = {
{, , , , },
{, , , , },
{, , , , },
{, , , , },
{, , , , },
};
const ll MU[][] = {
{, , , , },
{, , , , },
{, , , , },
{, , , , },
{, , , , },
};
const ll v1[] = {, , , , };
struct Matrix {
ll body[][]; Matrix() {}
Matrix(bool x) {
if (x) {
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
body[i][j] = ONE[i][j];
} else {
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
body[i][j] = MU[i][j];
}
} Matrix operator * (const Matrix &x) {
Matrix res;
ll tmp = ;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++) {
tmp = ;
for (int k = ; k < ; k++)
tmp = (tmp+(body[i][k]*x.body[k][j])%MOD)%MOD;
res.body[i][j] = tmp;
}
return res;
}
}; Matrix pow(int k){
Matrix res(true);
Matrix d(false);
while (k>) {
if (k&) res = res*d;
d = d*d;
k >>= ;
}
return res;
} int n;
ll v2[]; int main() {
#ifdef Phantom01
freopen("WHU1540.txt", "r", stdin);
#endif // Phantom01 while (scanf("%d", &n)!=EOF) {
if (==n) break; if (n<=) {
printf("%d\n", n);
continue;
}
Matrix tmp = pow(n-);
ll t = ;
for (int i = ; i < ; i++) {
t = ;
for (int j = ; j < ; j++)
t = (t+(tmp.body[i][j]*v1[j])%MOD)%MOD;
v2[i] = t;
}
printf("%d\n", (int)v2[]);
} return ;
}

WHU 1540 Fibonacci 递推的更多相关文章

  1. SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)

    Problem description. The Fibonacci numbers defined as f(n) = f(n-1) + f(n-2) where f0 = 0 and f1 = 1 ...

  2. Codeforces1065G Fibonacci Suffix 【递推】【二分答案】

    题目分析: 首先为了简便起见我们把前$15$的答案找出来,免得我们还要特判$200$以内之类的麻烦事. 然后我们从$16$开始递推.考虑猜测第i位是$0$还是$1$(这本质上是个二分).一开始先猜是$ ...

  3. POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】

    题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #includ ...

  4. Codeforces Gym101205D:Fibonacci Words(KMP+递推)

    Gym 101205D 题意:f[0] = "0", f[1] = "1",接下来f[i] = f[i-1] + f[i-2],相当于字符串拼接.然后给出一个n ...

  5. 牛客多校第九场 A The power of Fibonacci 杜教bm解线性递推

    题意:计算斐波那契数列前n项和的m次方模1e9 题解: $F[i] – F[i-1] – F[i-2] = 0$ $F[i]^2 – 2 F[i-1]^2 – 2 F[i-2]^2 + F[i-3] ...

  6. 斐波那契数列 递归 尾递归 递推 C++实现

    ==================================声明================================== 本文原创,转载请注明作者和出处,并保证文章的完整性(包括本 ...

  7. hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...

  8. hdu 1723 DP/递推

    题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...

  9. 51nod1149 Pi的递推式

    基准时间限制:1 秒 空间限制:131072 KB 分值: 640 F(x) = 1 (0 <= x < 4) F(x) = F(x - 1) + F(x - pi) (4 <= x ...

随机推荐

  1. (转载)打开一个本地apk进行安装

    1 2 3 4 5 6 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File ...

  2. 51nod 1101 换零钱 完全背包的变型 动态规划

    题目: 思路: ;i < ; i++){ for(int j = a[i];j <= n; j++){ dp[j] = (dp[j] + dp[j-a[i]])%mod; } } a[i] ...

  3. ActiveMQ学习笔记(9)----ActiveMQ静态网络连接

    1. 启动多个Broker 在win10下同一台服务器启动多个Broker, 步骤如下: 1. 复制安装目录下的conf文件夹命名为conf2 2. 修改activemq.xml中的brokerNam ...

  4. NetworkX-画图

    参考:https://blog.csdn.net/qq951127336/article/details/54586869 1.创建图 networkx有四种图 Graph .DiGraph.Mult ...

  5. Quartz任务调度 服务日志+log4net打印日志+制作windows服务

    引言 现在许多的项目都需要定时的服务进行支撑,而我们经常用到的定时服务就是Quartz任务调度了.不过我们在使用定时Job进行获取的时候,有时候我们就需要记录一下自定义的日志,甚至我们还会对执行定时J ...

  6. makefile--回顾基础篇

    前阵子让写makefile,纠结了下,基本忘记差不多了. 1.gcc的编译选项 -c 只是编译不链接,生成目标文件“.o” -S 只是编译不汇编,生成汇编代码 -E 只进行预编译,不做其他处理 -g ...

  7. 使用systemctl自定义系统服务

    1.创建系统服务文件,格式如下: [Unit] Description=httpd After=network.target [Service] Type=forking ExecStart=/usr ...

  8. System.IO.IsolatedStorage 使用 IsolatedStorageFileStream 存储信息

    在C#中还有一种叫做IsolatedStorage的存储机制,他存储信息的方式类似于我们的cookie, IsolatedStorage存储独立于每一个application,换句话说我们加载多个应用 ...

  9. 【codeforces 816A】Karen and Morning

    [题目链接]:http://codeforces.com/contest/816/problem/A [题意] 让你一分钟一分钟地累加时间; 问多长时间以后是个回文串; [题解] reverse之后如 ...

  10. Regular Expressions Syntax

    https://support.syslogwatcher.com/support/solutions/articles/8000033627-regular-expressions-syntax