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; //总的 ...
随机推荐
- ipvsadm分发MySQL读请求
在MySQL的部署场景中,经常使用HAproxy和ipvs来作为读请求转发的网关.ipvs的好处在于本身不需要daemon的方式来运行,而是直接作为kernel的服务来提供:当ipvs和应用程序服务器 ...
- LeetCode(290) Word Pattern
题目 Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- poj-3278 catch that cow(搜索题)
题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- HDU - 4763 Theme Section (KMP的next数组的应用)
给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...
- Mysql中的联合索引、前缀索引、覆盖索引
索引 索引是一种特殊的文件,它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度. 联合索引 又名复合索引,由两个或多个列的索引.它规定了mys ...
- JAVA、JDK等入门概念,下载安装JAVA并配置环境变量
一.概念 Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言,具体介绍可查阅百度JAVA百科,这里不再赘述. Java分为三个体系,分别为: Java SE(J2SE,Java2 Platf ...
- UVa 1629 DP Cake slicing
题意: 一块n×m的蛋糕上有若干个樱桃,要求切割若干次以后,每块蛋糕上有且仅有1个樱桃.求最小的切割长度. 分析: d(u, d, l, r)表示切割矩形(u, d, l, r)所需要的最小切割长度. ...
- day05 模块以及内置常用模块用法
内置常用模块详解: 1 time 2 datetime 3 random 4 os 5 sys 6 shutil 7 shelve 8 xml 9 configparser 10 hashlib ...
- poj2449 Remmarguts' Date K短路 A*
K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...
- 【转载】logistic回归
原文地址:https://www.cnblogs.com/zichun-zeng/p/3824745.html 1. logistic回归与一般线性回归模型的区别: (1) 线性回归的结果变量 ...