Gauss Fibonacci

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3149    Accepted Submission(s): 1323

Problem 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
 
这个题要利用的东西挺多的。刚开始连斐波拉契的矩阵都弄错了,看来还是要加强训练!

把斐波那契数列转化为矩阵:
A={1 1}
  {1,0};

{f[n+1],f[n]}  
{f[n],f[n-1]} = A^n ;最后输出M.v[1][0]这就是构造斐波拉契数列的矩阵了。

这个题将式子化简之后我们可以得到
sum(f(g(n))) = A^(b)+A^(b+k)+...+A^(b+(n-1)*k) = A^b * (1+A^k+A^2k+...A^(n-1)k)
利用递归函数求解 A^k+A^2k+...A^(n-1)k 然后与 单位矩阵相加,最后还要判断 b是否为 0
很巧妙。
这里给出一个等比数列的求和公式.
LL cal(int p,int n){  ///这里是递归求解等比数列模板 1+p+p^2...+p^n
if(n==) return ;
if(n&){//(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1));
return (+pow_mod(p,n/+))*cal(p,n/)%mod;
}
else { //(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2);
return (pow_mod(p,n/)+(+pow_mod(p,n/+))*cal(p,n/-))%mod;
}
}
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
struct Martix{
LL v[][];
}res;
LL k,b,n,M; Martix mult(Martix a,Martix b){
Martix temp;
for(int i=;i<;i++){
for(int j=;j<;j++){
temp.v[i][j] = ;
for(int k=;k<;k++){
temp.v[i][j] = (temp.v[i][j]+a.v[i][k]*b.v[k][j])%M;
}
}
}
return temp;
} Martix add(Martix a,Martix b){
for(int i=;i<;i++){
for(int j=;j<;j++){
a.v[i][j]=(a.v[i][j]+b.v[i][j])%M;
}
}
return a;
}
Martix pow_mod(Martix a,LL k){
Martix ans;
ans.v[][]=ans.v[][] = ;
ans.v[][]= ans.v[][]=;
while(k){
if(k&) ans = mult(ans,a);
a=mult(a,a);
k>>=;
}
return ans;
} Martix cal(Martix p,LL k) ///用二分法求矩阵和,递归实现 计算 a^1+a^2.....+a^p
{
if(k==)
return p;
else if(k&)
return add(cal(p,k-),pow_mod(p,k));
else
return mult(cal(p,k>>),add(pow_mod(p,k>>),res));
} int main(){ Martix a,t;
a.v[][] = a.v[][] = a.v[][] = ; ///斐波拉契数列的特征值矩阵[1 1 1 0]
a.v[][] = ;
t.v[][] = t.v[][] = ; ///单位矩阵
t.v[][] = t.v[][] = ;
while(scanf("%lld%lld%lld%lld",&k,&b,&n,&M)!=EOF){
Martix M1 = pow_mod(a,k);
res = t;
res = add(t,cal(M1,n-));
if(b!=){
Martix M3 = pow_mod(a,b);
res = mult(res,M3);
}
printf("%d\n",res.v[][]%M);
}
return ;
}
 

hdu 1588(矩阵好题+递归求解等比数列)的更多相关文章

  1. HDU 5694——BD String——————【递归求解】

    BD String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. 九度OJ1205题-递归求解问题

    题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5887 解决:2446 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用 ...

  3. HDU 1588 矩阵快速幂 嵌套矩阵

    这个题目搞了我差不多一个下午,之前自己推出一个公式,即 f[n+k]=k*f[n]+f[n-1]结果发现根本不能用,无法降低复杂度. 后来又个博客的做法相当叼,就按他的做法来了 即 最终求得是 S(n ...

  4. HDU - 1588 矩阵前缀和

    题意:给定\(k,b,n,m\),求\(\sum_{i=0}^{n-1}f(g(i))\) 其中\(f(i)=f(i-1)+f(i-2),f(1)=1,f(0)=0\),\(g(i)=k*i+b\) ...

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

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

  6. eetCode刷题-递归篇

    递归是算法学习中很基本也很常用的一种方法,但是对于初学者来说比较难以理解(PS:难点在于不断调用自身,产生多个返回值,理不清其返回值的具体顺序,以及最终的返回值到底是哪一个?).因此,本文将选择Lee ...

  7. HDU 4472 Count(数学 递归)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4472 Problem Description Prof. Tigris is the head of ...

  8. C++递归求解N个元素的所有子集

    C++递归求解N个元素的所有子集 引言: 我在复习C++遇到了设计递归函数的问题.这个例子,很好的显示了设计递归的方式,思想. 这与斐波那数列不同,这个例子更有应用意义. 问题: 试编写一个递归函数, ...

  9. N皇后问题——递归求解

    比较简单,废话不说,上代码: public class NQueen { //比如:position[1]=3,表示第一行的第三列有一个皇后 private int [] position; //总的 ...

随机推荐

  1. TypeError: cannot use a string pattern on a bytes-like object

    一劳永逸解决:TypeError: cannot use a string pattern on a bytes-like object TypeError: cannot use a string ...

  2. Linux学习-工作管理 (job control)

    什么是工作管理? 进行工作管理的行为中, 其实每个工作都是目前 bash 的 子进程,亦即彼此之间是有相关性的. 我们无法以 job control 的方式由 tty1 的环境去管理 tty2 的 b ...

  3. ROM,PROM,EPROM,EEPROM及FLASH存储器的区别

    在微机的发展初期,BIOS都存放在ROM(Read Only Memory,只读存储器)中.ROM内部的资料是在ROM的制造工序中,在工厂里用特殊的方法被烧录进去的,其中的内容只能读不能改,一旦烧录进 ...

  4. UVa 465 Overflow——WA

    上次那个大数开方的高精度的题,UVa113 Power of Cryptography,直接两个double变量,然后pow(x, 1 / n)就A过去了. 怎么感觉UVa上高精度的题测试数据不给力啊 ...

  5. HTML5之中国象棋,附带源码!

    好久没写随笔了,好怀恋2013年的日子,因为现在不能回到过去了! 再见了 感谢你为我做的一切! 进入正题:HTML5之中国象棋 很小就会下象棋了,  这是象棋的测试地址:点击我吧   然后点击里面的象 ...

  6. tab选项卡不同样式的效果

    一般的tab选项卡就只能两种样式,一种是选中或者是划过这个选项卡样式,一种是没选中或者没划过选项卡样式. 现在有这种需求,就是选中或划过tab选卡要不同样式.比如tab1选中或者划过是红色,tab2选 ...

  7. 30行js让你的rem弹性布局适配所有分辨率(含竖屏适配)

    用rem来实现移动端的弹性布局是个好主意!用法如下: CSS @media only screen and (max-width: 320px), only screen and (max-devic ...

  8. 第3章 jquery中的事件和动画

    一,jquery中的事件 (1).执行时机 $(document).ready()和window.onload方法有相似的功能,但是在执行时机方面有区别,windwo.onload在网页中所有的元素包 ...

  9. Python字典类型、

    字典类型: # msg_dic = {#     'apple': 10,#     'tesla': 100000,#     'mac': 3000,#     'lenovo': 30000,# ...

  10. IO Streams:格式化

    简介 实现格式化接口的流对象是PrintWriter,字符流类或PrintStream(字节流类). 注意:您可能唯一需要的PrintStream对象是System.out和System.err. ( ...