根据题意:最后一步是寻求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. Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程

    原文:Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程 Red Gate系列之八 SQL Connect 1.1.1.19 ...

  2. 【Android开发经验】Android举UI设计经验

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 1.Android眼下的主流设备分辨率为480×800.720×1280.1080×1920,单位是像素.在 ...

  3. 王立平--android发育,转让eclipse可选颜色

    android:background="@android:color/white" 版权声明:本文博主原创文章.博客,未经同意不得转载.

  4. _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...

  5. 如何将IPhone应用软件发布到App Store的

    转自:http://www.shtion.com/667.html 怎样将IPhone应用程序软件公布到应用程序商店? 2009年10月19日公布 分类: App store, iphone, 手机应 ...

  6. 对XSD schema文件中elementFormDefault属性的理解

    Schema中的elementFormDefault elementFormDefault取值:qualified 或者 unqualified 在http://www.velocityreviews ...

  7. richedit设置滚动条的位置和更新内容

    需要txt发现读者richedit的scrollbar位置(为了便于下一次读,直接访问与上次读取下一个读取位置)不值得治疗,采用GetScrollPos.SetScrollPos你可以设置scorll ...

  8. YT新人之巅峰大决战03

    题目链接 Problem Description Now give you two integers n m, you just tell me the m-th number after radix ...

  9. CF(427D-Match &amp; Catch)后缀数组应用

    题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等于1.出现的定义为:能够重叠的出现. 解法:后缀数组的应用.从小枚举长度.假设一个长度len合法的话:则一定存在这个样的s ...

  10. RedHat Linux乱码解决方案(转)

    RedHat Linux中出现中文乱码主要是由于没有安装中文字体,因此解决方案主要是安装中文字体,所以 第一步,挂载安装的光盘 在虚拟机的菜单栏里,选择:VM->Settings,点击Setti ...