Gauss Fibonacci

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

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

题目要求求出f(g(i))的总和,i是0~n-1

代码中详细思路+注释

/*f(g(i))=f(k*i+b)
令f[n]=A;//A是矩阵,A的某个元素是F[n]
若i=0~n-1,则sum(f(k*i+b))
=A^b+A^(k+b)+A^(2k+b)+A^(3k+b)+...+A^((n-1)k+b)
=A^b+A^b(A^k+A^2k+A^3k+A^4k+...+A^(n-1)k)
将A^k看成一个新的矩阵B,则原式:
=A^b+A^b(B^1+B^2+B^3+...+B^(n-1));//A^b,A^k用矩阵快速幂求出,括号中的用二分矩阵可求
所谓二分矩阵:A^1+A^2+A^3+A^4+A^5+A^6=(A^1+A^2+A^3)+A^3(A^1+A^2+A^3)
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=2;
__int64 array[MAX][MAX],sum[MAX][MAX],temp[MAX][MAX],ans[MAX][MAX];
//array相当于A,sum记录每次幂乘后的矩阵,temp是临时矩阵,ans是B^1+B^2+B^3+...+B^n void MatrixInit(__int64 a[MAX][MAX],bool flag){//初始化矩阵
for(int i=0;i<MAX;++i){
for(int j=0;j<MAX;++j){
if(flag)a[i][j]=array[i][j];//a=A
else a[i][j]=(i == j);//a=1
}
}
} void MatrixAdd(__int64 a[MAX][MAX],__int64 b[MAX][MAX],int &mod){//矩阵相加
for(int i=0;i<MAX;++i){//a=a+b
for(int j=0;j<MAX;++j){
a[i][j]=(a[i][j]+b[i][j])%mod;
}
}
} void MatrixMult(__int64 a[MAX][MAX],__int64 b[MAX][MAX],int &mod){//矩阵相乘
__int64 c[MAX][MAX]={0};
for(int i=0;i<MAX;++i){//a=a*b
for(int j=0;j<MAX;++j){
for(int k=0;k<MAX;++k){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<MAX;++i){
for(int j=0;j<MAX;++j)a[i][j]=c[i][j]%mod;
}
} void MatrixPow(int k,int &mod){//矩阵幂乘,sum=A^k
MatrixInit(sum,0);//sum=1
MatrixInit(temp,1);//temp=A
while(k){
if(k&1)MatrixMult(sum,temp,mod);
MatrixMult(temp,temp,mod);
k>>=1;
}
} void MatrixSum(int k,int &mod){//矩阵求和
if(k == 1){MatrixInit(ans,1);return;}
MatrixSum(k/2,mod);
MatrixPow((k+1)/2,mod);
if(k&1){//k为奇数则A+(A+A^m)*(A+A^2+A^3...),m=(k+1)/2
MatrixInit(temp,1);//temp=A
MatrixAdd(sum,temp,mod);//sum=A+A^m
MatrixMult(ans,sum,mod);//ans=sum*ans
MatrixAdd(ans,temp,mod);//ans=A+ans
}
else{//k为偶数则(1+A^m)*(A+A^2+A^3...),m=(k+1)/2
MatrixInit(temp,0);//temp=1
MatrixAdd(temp,sum,mod);//temp=1+A^m
MatrixMult(ans,temp,mod);//ans=ans*temp;
}
} int main(){
int k,b,n,m;
while(scanf("%d%d%d%d",&k,&b,&n,&m)!=EOF){
array[0][0]=array[0][1]=array[1][0]=1;
array[1][1]=0;
MatrixPow(k,m);//求A^k
MatrixInit(array,0);
MatrixMult(array,sum,m);//将array构造成B,即A^k
MatrixSum(n-1,m);//求矩阵和
array[0][0]=array[0][1]=array[1][0]=1;
array[1][1]=0;
MatrixPow(b,m);//求A^b;
MatrixMult(ans,sum,m);//求A^b*ans
MatrixAdd(ans,sum,m);//求A^b+A^b+ans
printf("%I64d\n",ans[1][0]);
}
return 0;
}

hdu1588之经典矩阵乘法的更多相关文章

  1. poj3233之经典矩阵乘法

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 12346   Accepted:  ...

  2. zoj3497(经典矩阵乘法)

    原以为是用搜索做的题,想了好久都无法想到一个高效正确的解法. 后面发现竟然这就是矩阵的应用! 碉堡! 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值  ——选自ma ...

  3. 学习心得:《十个利用矩阵乘法解决的经典题目》from Matrix67

    本文来自:http://www.matrix67.com/blog/archives/tag/poj大牛的博文学习学习 节选如下部分:矩阵乘法的两个重要性质:一,矩阵乘法不满足交换律:二,矩阵乘法满足 ...

  4. 【转】Matrix67:十个利用矩阵乘法解决的经典题目

    好像目前还没有这方面题目的总结.这几天连续看到四个问这类题目的人,今天在这里简单写一下.这里我们不介绍其它有关矩阵的知识,只介绍矩阵乘法和相关性质.    不要以为数学中的矩阵也是黑色屏幕上不断变化的 ...

  5. 【矩阵乘法经典应用】【ZOJ3497】【Mistwa】

    题意:给定一个有向图(最多25个节点,每个节点的出度最多为4),给定起点和终点,然后从起点开始走,走到终点就停止,否则一直往下走,问能不能P步到达终点.也就是说从起点出发,走一条长度为P的路径,路径中 ...

  6. CH Round #30 摆花[矩阵乘法]

    摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...

  7. 【BZOJ-1898】Swamp 沼泽鳄鱼 矩阵乘法

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1012  Solved: 566[Submit][S ...

  8. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

  9. 如何使用矩阵乘法加速动态规划——以[SDOI2009]HH去散步为例

    对这个题目的最初理解 开始看到这个题,觉得很水,直接写了一个最简单地动态规划,就是定义 f[i][j]为到了i节点路径长度为j的路径总数, 转移的话使用Floyd算法的思想去转移,借助这个题目也理解了 ...

随机推荐

  1. 什么是Elasticsearch

    一个采用Restfull API 标准的高扩展性和高可用性的实时数据分析的全文搜索工具 Elasticsearch 涉及到的一些概念: 1.Node(节点): 单个的装有Elasticsearch服务 ...

  2. 【转】Linux驱动模块编译进内核中

    原文网址:http://blog.chinaunix.net/uid-29287950-id-4573481.html BQ27501驱动编译进内核 一.       驱动程序编译进内核的步骤 在 l ...

  3. web前端设计:JQuery MINI UI

    JQuery MINIUI 个人感觉用起来很爽,所以在此记录之,以后开发过程可能作为备选项.它能缩短开发时间,减少代码量,使开发者更专注于业务和服务端,轻松实现界面开发,带来绝佳的用户体验.在线下载地 ...

  4. 剑指offer-面试题12.打印1到最大的n位数

    题目:输入数字n,按照打印出从1最大的n位10进制数.比如3,则 打印出1.2.3一直到最大的3位数即999 1.你觉得如果面试会有这么简单的题,那 只能说明你---太天真. 2.n=3尚可,如果n= ...

  5. OpenJudge Trans

    #include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include ...

  6. 解决红米等手机(移动端)无法触发touchend事件

    触屏事件的简单描述: js的触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是 pageX和 pageY,表示X坐标,Y坐标.touchs ...

  7. Visual Studio® 2010 Web Deployment Projects站点编译生成bin同时发表插件

    VS2010环境下: 1.Visual Studio® 2010 Web Deployment Projects下载地址:        http://www.microsoft.com/downlo ...

  8. 第五章 Logistic回归

    第五章 Logistic回归 假设现在有一些数据点,我们利用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归. 为了实现Logistic回归分类器,我们可以在每个特征上都乘以一 ...

  9. 【温故而知新】Tcp/Ip协议——总览

    Tcp/Ip协议 一.概念 “三网”:即电信网络.有线电视网络和计算机网络 标准(Standards) 描述了协议的规定,设定了最简的性能集. 协议(Protocol) 网络设备用于交换信息的系列规则 ...

  10. 关于使用Html5 canvas、 map、jquery构造不规则变色点击区域 热点区域

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...