您的位置:首页 > 其它

HDU 1588 Gauss Fibonacci(矩阵快速幂)

2017-01-17 13:09 316 查看

Gauss Fibonacci

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27 Accepted Submission(s): 5

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.

SampleInput

2 1 4 100
2 0 4 100


SampleOutput

21
12

by yxt

用于构造斐波那契的矩阵为

1,1

1,0

设这个矩阵为A。

sum=f(b)+f(k+b)+f(2*k+b)+f(3*k+b)+........+f((n-1)*k+b)

<=>sum=A^b+A^(k+b)+A^(2*k+b)+A^(3*k+b)+........+A^((n-1)*k+b)

<=>sum=A^b+A^b*(A^k+A^2*k+A^3*k+.......+A^((n-1)*k)) (1)

设矩阵B为A^k;

那么(1)式为 sum=A^b+A^b*(B+B^2+B^3+......+B^(n-1));

显然,这时候就可以用二分矩阵做了,括号内的就跟POJ 3233的形式一样了。

代码如下

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL __int64
LL mod;
struct matrix
{
LL ma[3][3];
}init, res1, res2, ans;
matrix Mult(matrix x, matrix y)//矩阵相乘
{
matrix tmp;
int i, j, k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
tmp.ma[i][j]=0;
for(k=0;k<2;k++)
{
tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;
}
}
}
return tmp;
}
matrix Pow(matrix x, int k) //矩阵快速幂关键
{
matrix tmp;
int i, j;
for(i=0;i<2;i++) for(j=0;j<2;j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
}
matrix Add(matrix x, matrix y)//矩阵相加
{
int i, j;
matrix tmp;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
tmp.ma[i][j]=(x.ma[i][j]+y.ma[i][j])%mod;
}
}
return tmp;
}
matrix Sum(matrix x, int k)//等比矩阵求和
{
if(k==1) return x;
if(k&1)
return Add(Sum(x,k-1),Pow(x,k));
matrix tmp;
tmp=Sum(x,k>>1);
return Add(tmp,Mult(tmp,Pow(x,k>>1)));
}
int main()
{
int k, b, n;
while(scanf("%d%d%d%d",&k,&b,&n,&mod)!=EOF)
{
init.ma[0][0]=1;
init.ma[0][1]=1;
init.ma[1][0]=1;
init.ma[1][1]=0;
res1=Pow(init,b);
res2=Pow(init,k);
ans=Add(res1,Mult(res1,Sum(res2,n-1)));
printf("%I64d\n",ans.ma[0][1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: