hdu 1588(矩阵好题+递归求解等比数列)
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3149 Accepted Submission(s): 1323
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.
Each of them will not exceed 1,000,000,000.
2 0 4 100
把斐波那契数列转化为矩阵:
A={1 1}
{1,0};
{f[n+1],f[n]}
{f[n],f[n-1]} = A^n ;最后输出M.v[1][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(矩阵好题+递归求解等比数列)的更多相关文章
- HDU 5694——BD String——————【递归求解】
BD String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 九度OJ1205题-递归求解问题
题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5887 解决:2446 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用 ...
- HDU 1588 矩阵快速幂 嵌套矩阵
这个题目搞了我差不多一个下午,之前自己推出一个公式,即 f[n+k]=k*f[n]+f[n-1]结果发现根本不能用,无法降低复杂度. 后来又个博客的做法相当叼,就按他的做法来了 即 最终求得是 S(n ...
- 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\) ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- eetCode刷题-递归篇
递归是算法学习中很基本也很常用的一种方法,但是对于初学者来说比较难以理解(PS:难点在于不断调用自身,产生多个返回值,理不清其返回值的具体顺序,以及最终的返回值到底是哪一个?).因此,本文将选择Lee ...
- HDU 4472 Count(数学 递归)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4472 Problem Description Prof. Tigris is the head of ...
- C++递归求解N个元素的所有子集
C++递归求解N个元素的所有子集 引言: 我在复习C++遇到了设计递归函数的问题.这个例子,很好的显示了设计递归的方式,思想. 这与斐波那数列不同,这个例子更有应用意义. 问题: 试编写一个递归函数, ...
- N皇后问题——递归求解
比较简单,废话不说,上代码: public class NQueen { //比如:position[1]=3,表示第一行的第三列有一个皇后 private int [] position; //总的 ...
随机推荐
- Spring AOP注解形式简单实现
实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...
- 前端MVVM模式及其在Vue和React中的体现
MVVM相关概念 Mvvm 前端数据流框架精讲 1) MVVM典型特点是有四个概念:Model.View.ViewModel.绑定器.MVVM可以是单向绑定也可以是双向绑定甚至是不绑定 2) 绑定器: ...
- kafka的初认识
学习地址: http://www.jikexueyuan.com/course/1716_3.html?ss=1 http://www.jikexueyuan.com/course/kafka/ zo ...
- 10个MCU常用的基础知识
转自:http://bbs.21ic.com/icview-2659278-1-1.html 1.MCU有串口外设的话,在加上电平转换芯片,如MAX232.SP3485就是RS232和RS485接口了 ...
- Aizu - 1386 Starting a Scenic Railroad Service (思维乱搞)
给你n个区间,求: 1:最多有多少区间与同一个区间相交. 2:相交部分的最大区间数目. Sample Input 1 4 1 3 1 3 3 6 3 6 Sample Output 1 2 2 Sam ...
- Linux学习-X Server 配置文件解析与设定
X server 的配置 文件都是预设放置在 /etc/X11 目录下,而相关的显示模块或上面提到的总总模块,则主要放置在/usr/lib64/xorg/modules . 比较重要的是字型文件与芯片 ...
- Android兼容性测试CTS Verifier-环境搭建、测试执行、结果分析
CTS Verifier算是CTS的一部分,需要手动进行,主要用于测试那些自动测试系统无法测试的功能,比如相机.传感器等.由于硬件配置或其他原因,不同手机上部分测试项目被隐藏,也就是说CTS Veri ...
- html-body相关标签
一 字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub& ...
- 关于freetype在安装中的遇到的问题
本人电脑配置的是Anconda环境+pycharm2017.2.2 comuniity,每次安装什么包就是直接pip install 的,但是这次在安装freetype的安装中却遇到了麻烦. 具体是在 ...
- JAVA 消耗 CPU过高排查方法
#找出cpu占用最高的进程top -H#再次确定进程ps aux|grep 17408 #查看进程的线程(tid) ps -mp 17408 -o THREAD,tid,time#将线程转换为十六进制 ...