Description

Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "


How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".


As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.




Arithmetic progression:

g(i)=k*i+b;

We assume k and b are both non-nagetive integers.



Fibonacci Numbers:

f(0)=0

f(1)=1

f(n)=f(n-1)+f(n-2) (n>=2)



The Gauss Fibonacci problem is described as follows:

Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n

The answer may be very large, so you should divide this answer by M and just output the remainder instead.
 

Input

The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M


Each of them will not exceed 1,000,000,000.
 

Output

For each line input, out the value described above.
 

Sample Input

2 1 4 100
2 0 4 100
 

Sample Output

21
12

题意:g(i)=k*i+b , 求全部的f(g(i))在n的范围里

思路:借鉴:构造矩阵:

       |1  1|      | f(2)    f(1)|

    A= |1  0|   =  | f(1)    f(0)|

         

           

     |1  1| ^b   | f(b+1)    f(b)|             

A^b =|1  0|  =   | f(b)    f(b-1)|

                 

    f(b) = matrix[0][1]=matrix[1][0];

                       

    首项是:A^b

    公比是:A^k

    项数是:N

    能够把问题进一步简化

    由于矩阵的加法对乘法也符合分配律,我们提出一个A^b来,形成这种式子:

    A^b*( I + A^k + (A^k)^2 + .... + (A^k)^(N-1) )

    A^b 和 A^k 显然都能够用我们之前说过的方法计算出来,这剩下一部分累加怎么解决呢

    设A^k=B

    要求 G(N)=I + ... + B^(N-1),

    i=N/2

    若N为偶数,G(N)=G(i)+G(i)*B^i = G(i) *( I+B^(i));

    若N为奇数,G(N)=I+ G(i)*B + G(i) * (B^(i+1)) = G(N-1)+B^N; (前一个等式可能要快点,可是后面更简练)

                                   

    我们来设置这样一个矩阵

    B I

    O I

    当中O是零矩阵,I是单位矩阵

    将它乘方,得到

    B^2 I+B

    O   I

    乘三方,得到

    B^3 I+B+B^2

    O   I

    乘四方,得到

    B^4  I+B+B^2+B^3

    O    I                                 

既然已经转换成矩阵的幂了,继续用我们的二分或者二进制法,直接求出幂就能够了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const int maxn = 2; int m;
struct Matrix {
ll v[maxn][maxn];
Matrix() {}
Matrix(int x) {
init();
for (int i = 0; i < maxn; i++)
v[i][i] = x;
}
void init() {
memset(v, 0, sizeof(v));
}
Matrix operator *(Matrix const &b) const {
Matrix c;
c.init();
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
for (int k = 0; k < maxn; k++)
c.v[i][j] = (c.v[i][j] + (v[i][k]*b.v[k][j])%m) % m;
return c;
}
Matrix operator ^(int b) {
Matrix a = *this, res(1);
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
} u, em; Matrix Add(Matrix a, Matrix b) {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
a.v[i][j] = (a.v[i][j]+b.v[i][j]) % m;
return a;
} Matrix BinarySum(Matrix a, int n) {
if (n == 1)
return a;
if (n & 1)
return Add(BinarySum(a, n-1), a^n);
else return BinarySum(a, n>>1) * Add(u, a^(n>>1));
} int main() {
int k, b, n;
u.init(), em.init();
u.v[0][0] = 1, u.v[0][1] = 0, u.v[1][0] = 0, u.v[1][1] = 1;
em.v[0][0] = 1, em.v[0][1] = 1, em.v[1][0] = 1, em.v[1][1] = 0;
while (scanf("%d%d%d%d", &k, &b, &n, &m) != EOF) {
Matrix t1, t2, ans;
t1 = em^b;
t2 = em^k;
ans = Add(u, BinarySum(t2, n-1)) * t1;
cout << ans.v[0][1] << endl;
}
return 0;
}

HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)的更多相关文章

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

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

  2. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

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

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

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

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

  5. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

  6. hdu 1588(Fibonacci矩阵求和)

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

  7. hdu 1588 Gauss Fibonacci(矩阵嵌矩阵)

    题目大意: 求出斐波那契中的 第 k*i+b 项的和. 思路分析: 定义斐波那契数列的矩阵 f(n)为斐波那契第n项 F(n) = f(n+1) f(n) 那么能够知道矩阵 A = 1 1 1  0 ...

  8. HDU 2604 Queuing(矩阵高速幂)

    题目地址:HDU 2604 这题仅仅要推出公式来,构造矩阵就非常easy了.问题是推不出公式来..TAT.. 从递推的思路考虑.用f(n)表示n个人满足条件的结果.假设最后一个是m则前n-1人能够随意 ...

  9. HDU 2604 Queuing,矩阵高速幂

    题目地址:HDU 2604 Queuing 题意:  略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...

随机推荐

  1. python学习——截图工具编写

    学习一门语言最好的方法便是实践,想要拿Python写一个截图工具,网上一搜资料果然已经很多,前辈们都已经做的很到位了.现在就一步步来学习一下: 首先学习截图整个桌面的方法,可以使用Python中的PI ...

  2. Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)

    1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...

  3. MapReduce整体架构分析

    继前段时间分析Redis源代码一段时间之后.我即将開始接下来的一段技术学习的征程.研究的技术就是当前很火热的Hadoop,可是一个Hadoop生态圈是很庞大的.所以首先我的打算是挑选当中的一部分模块, ...

  4. 让Delphi XE2程序支持UAC

    在win7下,开发的程序有的时候莫名其妙就不能正常工作了,其实都是因为权限不够,要想能够正常运行,就需要获得管理员权限,这就需要处理UAC.具体方法如下: 一,制作“uac.manifest”文件.新 ...

  5. DELPHI XE7 新的并行库

    DELPHI XE7 的新功能列表里面增加了并行库System.Threading, System.SyncObjs. 为什么要增加新的并行库? 还是为了跨平台.以前要并行编程只能从TThread类继 ...

  6. C# MVC 自学笔记—2 MVC Movie简介

    MVC Movie是微软官方的一个MVC入门项目,我们可以跟着这个项目来实践入门 这是官方地址 http://www.asp.net/mvc/tutorials/mvc-4/getting-start ...

  7. git 提交ignore files

    1,首先在命令行创建.gitignore文件 $ touch .gitignore 2,在文件.gitignore 加入要忽略的文件入 $ echo *.class > .gitignore 3 ...

  8. 贴一个CMemDC 代码,这东西真不错噢,短小精悍,可谓极品

    罗索客 发布于 2006-11-28 21:53 点击:3941次  来自: 原文: http://yuantao82.spaces.live.com/Blog/cns!8FC0A772D812A22 ...

  9. 小米手机usb共享网络mac

    今天.我想去FQgoogle,mac在墙上,不便于使用.甚至只是用Android手机wifi,打开墙软件.然后usb分享到mac.然后mac互联网. 在谈到一些复杂.其实,关键一,小米手机usb共享, ...

  10. rsyslog+LogAnalyzer 日志收集

    Linux 之rsyslog+LogAnalyzer 日志收集系统 一.LogAnalyzer介绍 LogAnalyzer工具提供了一个易于使用,功能强大的前端,用于搜索,查看和分析网络活动数据,包括 ...