题目链接:https://vjudge.net/problem/UVA-10689

题解:

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
//const int MOD = 1e9+7;
const int MAXN = 1e6+; LL MOD;
const int Size = ;
struct MA
{
LL mat[Size][Size];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} MA tmp = {
, ,
,
}; int main()
{
LL f[], n, m;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld%lld%lld",&f[],&f[],&n,&m);
if(n<)
{
printf("%lld\n", f[n]);
continue;
} MOD = ;
while(m--) MOD *= ;
MA s = tmp;
s = qpow(s, n-);
LL ans = (1LL*s.mat[][]*f[]%MOD+1LL*s.mat[][]*f[]%MOD)%MOD;
printf("%lld\n", ans);
}
}

UVA10689 Yet another Number Sequence —— 斐波那契、矩阵快速幂的更多相关文章

  1. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  2. 「GXOI / GZOI2019」逼死强迫症——斐波那契+矩阵快速幂

    题目 [题目描述] ITX351 要铺一条 $2 \times N$ 的路,为此他购买了 $N$ 块 $2 \times 1$ 的方砖.可是其中一块砖在运送的过程中从中间裂开了,变成了两块 $1 \t ...

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

    题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...

  4. 51Nod - 1242 斐波那契(快速幂)

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

  5. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  6. Computational Complexity of Fibonacci Sequence / 斐波那契数列的时空复杂度

    Fibonacci Sequence 维基百科 \(F(n) = F(n-1)+F(n-2)\),其中 \(F(0)=0, F(1)=1\),即该数列由 0 和 1 开始,之后的数字由相邻的前两项相加 ...

  7. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  8. HDU5950 Recursive sequence 非线性递推式 矩阵快速幂

    题目传送门 题目描述:给出一个数列的第一项和第二项,计算第n项. 递推式是 f(n)=f(n-1)+2*f(n-2)+n^4. 由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题 ...

  9. DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂

    It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...

随机推荐

  1. HTTP基础认证Basic Authentication

    HTTP基础认证Basic Authentication Basic Authentication是一种HTTP访问控制方式,用于限制对网站资源的访问.这种方式不需要Cookie和Session,只需 ...

  2. Maven修改默认中央仓库

    其实Maven的默认仓库是可以修改的.比如使用阿里云的镜像地址等. 修改步骤: 1.打开{M2_HOME}/conf/settings.xml文件,找到mirrors节点,修改如下代码: <mi ...

  3. 第四章——SQLServer2008-2012资源及性能监控(1)专家

    http://blog.csdn.net/dba_huangzj/article/details/8614817

  4. MVC项目引用备注

    新建空的WEB应用 MVC Nuget引用:Microsoft.AspNet.MvcMicrosoft.AspNet.Web.Optimization 可选删除 Microsoft.CodeDom.P ...

  5. 100 Most Influential Books According to Stack Overflow

    Please read this blog post to see why this is here. This data was created on 02/13/2012 20:00:00 All ...

  6. Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息。

    Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息.”的问题解决 2015-01-22 09:49:02 标签:Windows Server2003 PerfDi ...

  7. Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?

    什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...

  8. go mysql dsn

    https://github.com/go-sql-driver/mysql#dsn-data-source-name DSN (Data Source Name) The Data Source N ...

  9. poj Kindergarten

    Kindergarten 又是一道自己没思考出来的题 !!!!! 还是老样子,题目去我拉的专题里有. 题目: 给出G给女孩,B给男孩.女孩之间是相互认识的,男孩之间也是相互认识的.如今题目中给出M对男 ...

  10. Crtmp Server 几个关键流程

    最近在阅读Crtmp Sever 源码,有些关键流程记录下来,以备以后查阅.假设rtmp播放地址是"rtmp://127.0.0.1/live/mystream live=1" 1 ...