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. NOIP2018 - 一些板子

    好多东西都不熟练…… 数论 数论分块「bzoj2956: 模积和」 10.28.2018 #include<bits/stdc++.h> typedef long long ll; ; ; ...

  2. paper:基于verilog HDL 的高速可综合FSM设计

    1.寄存器输出型状态机 VS 组合逻辑输出型状态机 2.状态编码方法 这块讲的不好,也比较少. 3.系统设计中模块划分的指导性原则

  3. 【linux】【rpm】确定程序是否 rpm 安装

    执行 rpm -qf 文件名如果结果显示出安装包那就说明是rpm (或者yum)安装 详情参看 rpm -v  (或者 man rpm) ​

  4. Tourists Gym - 101002I LCA——dfs+RMQ在线算法

    LCA(Least Common Ancestors),即最近公共祖先,是指这样一个问题:在有根树中,找出某两个结点u和v最近的公共祖先(另一种说法,离树根最远的公共祖先). 知识需求:1)RMQ的S ...

  5. Linux下open函数、read函数、write函数记录

    open() #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int open( cons ...

  6. hdu3487Play with Chain

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. GridView的RowCommand事件中获取每行控件的值

    //获取当前行                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent; //获取 ...

  8. rocketmq源码分析2-broker的消息接收

    broker消息接收,假设接收的是一个普通消息(即没有事务),此处分析也只分析master上动作逻辑,不涉及ha. 1. 如何找到消息接收处理入口 可以通过broker的监听端口10911顺藤摸瓜式的 ...

  9. linux随笔4

    vim编辑器: 启动vim编辑器,只需键入vim 和希望编辑的文件:vim mongo.sh 如果文件存在,将显示整个内容显示到进行编辑的缓冲区,如果文件不存在,打开一个新的缓冲区进行编辑. 内容未占 ...

  10. Python-函数参数的传递

    作者:Vamei 出处:http://www.cnblogs.com/vamei,感谢博主的分享, python的函数参数传递有这样的几种形式: 1.位置传递 2.关键字传递 3.参数默认值传递 4. ...