根据题意:最后一步是寻求f(b) + f(k + b) + f(2 * k + b) + …+ f((n-1) * k + b)

清除f(b) = A^b

间A =

1 1

1 0

所以sum(n - 1) = A^b(E + A^ k + A ^(2 * k) + … + A ^((n - 1) * k)

设D = A^k

sum(n-1) = A^b(E + D + D ^ 2 + … + D ^(n - 1))

括号中的部分就能够二分递归求出来了

而单个矩阵就能够用矩阵高速幂求出来

/*************************************************************************
> File Name: hdu1588.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月12日 星期四 18时25分07秒
************************************************************************/ #include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL; LL mod, k, b; class MARTIX
{
public:
LL mat[3][3];
MARTIX();
MARTIX operator * (const MARTIX &b)const;
MARTIX operator + (const MARTIX &b)const;
MARTIX& operator = (const MARTIX &b);
}A, E, D; MARTIX :: MARTIX()
{
memset (mat, 0, sizeof(mat));
} MARTIX MARTIX :: operator * (const MARTIX &b)const
{
MARTIX ret;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 2; ++k)
{
ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
ret.mat[i][j] %= mod;
}
}
}
return ret;
} MARTIX MARTIX :: operator + (const MARTIX &b)const
{
MARTIX ret;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
ret.mat[i][j] = this -> mat[i][j] + b.mat[i][j];
ret.mat[i][j] %= mod;
}
}
return ret;
} MARTIX& MARTIX :: operator = (const MARTIX &b)
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
this -> mat[i][j] = b.mat[i][j];
}
}
return *this;
} MARTIX fastpow(MARTIX ret, LL n)
{
MARTIX ans;
ans.mat[0][0] = ans.mat[1][1] = 1;
while (n)
{
if (n & 1)
{
ans = ans * ret;
}
n >>= 1;
ret = ret * ret;
}
return ans;
} void Debug(MARTIX A)
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
printf("%lld ", A.mat[i][j]);
}
printf("\n");
}
} MARTIX binseach(LL n)
{
if (n == 1)
{
return D;
}
MARTIX nxt = binseach(n >> 1);
MARTIX B = fastpow(D, n / 2);
B = B + E;
nxt = nxt * B;
if (n & 1)
{
MARTIX C = fastpow(D, n);
nxt = nxt + C;
}
return nxt;
} int main()
{
LL n;
E.mat[0][0] = E.mat[1][1] = 1;
A.mat[0][0] = A.mat[0][1] = A.mat[1][0] = 1;
// Debug(A);
while (~scanf("%lld%lld%lld%lld", &k, &b, &n, &mod))
{
if (n == 1)
{
MARTIX x = fastpow(A, b);
printf("%lld\n", x.mat[0][1]);
continue;
}
D = fastpow(A, k);
MARTIX ans = binseach(n - 1);
ans = ans + E;
MARTIX base = fastpow(A, b);
ans = base * ans;
// Debug(ans);
printf("%lld\n", ans.mat[0][1]);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

hdu1588---Gauss Fibonacci(矩阵,线性复发)的更多相关文章

  1. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  2. POJ3233]Matrix Power Series && [HDU1588]Gauss Fibonacci

    题目:Matrix Power Series 传送门:http://poj.org/problem?id=3233 分析: 方法一:引用Matrix67大佬的矩阵十题:这道题两次二分,相当经典.首先我 ...

  3. HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)

    Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very cle ...

  4. HDU:Gauss Fibonacci(矩阵快速幂+二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=1588 Problem Description Without expecting, Angel replied ...

  5. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  6. hdu1588之经典矩阵乘法

    Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 1588(Fibonacci矩阵求和)

    题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...

  8. BZOJ3286 Fibonacci矩阵 矩阵 快速幂 卡常

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3286 题意概括 n,m,a,b,c,d,e,f<=10^1000000 题解 神奇的卡常题目 ...

  9. hdu1588:Gauss Fibonacci

    对每个0<=i<n求f(g(i))的和,其中f(x)为斐波那契数列第x项,g(i)=k*i+b,k,b,n给定,模数给定. 斐波那契数有一种用矩阵乘法求的方法,这个矩阵A自己写,令F[i] ...

随机推荐

  1. WebPack实例与前端性能优化

    [前端构建]WebPack实例与前端性能优化   计划把微信的文章也搬一份上来. 这篇主要介绍一下我在玩Webpack过程中的心得.通过实例介绍WebPack的安装,插件使用及加载策略.感受构建工具给 ...

  2. 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

    原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...

  3. Android各种屏幕分辨率(VGA、HVGA、QVGA、WQVGA、WVGA、FWVGA) 具体解释

    看资料的时候常常看到各种VGA,全都混了,无奈,找了些资料总结了下,分享给大家: 这些术语都是指屏幕的分辨率. VGA:Video Graphics Array,即:显示画图矩阵,相当于640×480 ...

  4. fork与vfork详解

    一.fork函数 要创建一个进程,最基本的系统调用是fork,系统调用fork用于派生一个进程,函数原型如下: pid_t fork(void)  若成功,父进程中返回子进程ID,子进程中返回0,若出 ...

  5. Makefile学习(一)[第二版]

    简单介绍 1)make:利用 make 工具能够自己主动完毕编译工作.这些工作包含:假设仅改动了某几个源文件,则仅仅又一次编译这几个源文件[make通过比对对应的.c文件与.o文件的时间];假设某个头 ...

  6. bootstarp modal自己主动调整宽度的JS代码

    $('#ajaxPage').modal('show').css({ width: 'auto', 'margin-left': function () { return -($(this).widt ...

  7. C# 几种方法来复制的阵列

    突然接触到,所以就写一下共享. 首先说明一下,数组是引用类型的,所以注意不要在复制时复制了地址而没有复制数值! 事实上在复制数组的时候.一定要用new在堆中开辟一块新的空间专门用于存放数组.这样才是有 ...

  8. 返璞归真 asp.net mvc (4) - View/ViewEngine

    原文:返璞归真 asp.net mvc (4) - View/ViewEngine [索引页] [源码下载] 返璞归真 asp.net mvc (4) - View/ViewEngine 作者:web ...

  9. 追索权 Eclipse + NDK error: stray &#39;\24&#39; in program

    [size=16px][b][color=#FF0000]追索权 Eclipse + NDK  error: stray '\24' in program[/color][b][/b][/b][/si ...

  10. linux于test 订购具体解释

    測试标志 代表意义 文件名称.文件类型 -e 该文件名称是否存在 -f 该文件名称是否存在且为file -d 该文件名称是否存在且为文件夹 -b 该文件名称是否存在且为一个block -c 该文件名称 ...