【HDU 1588】Gauss Fibonacci(矩阵快速幂+二分)

原创 2016年06月01日 17:44:24

Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3144 Accepted Submission(s): 1319

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

[题意][求n1i=0 f[k*i+b] % p 的值(f代表斐波那契数列)]
【题解】【矩阵快速幂】
A={{1,1},{1,0}},B={{1,0},{0,1}}

n1i=0 f[k*i+b]
=Ab+Ak+b+A2k+b++A(n1)k+b
=Ab(1+Ak+A2k+A3k++A(n1)k)
设:B=Ak
∴ =Ab(B0+B1+B2+B3++Bn1)
由本题的范围可以得知,不可能直接枚举,所以需要优化时间,二分+递归无疑是最显然的方法……
又∵ 对于矩阵Sm=F1+F2+F3++Fm
mS(m)=(1+Fm/2)(F+F2+F3++Fm/2)
mS(m)=F+(F+Fm/2+1)(F+F2++Fm/2)
∴ 在本题中,式子最终化为 n1i=0 f[k*i+b]=Ab(B0+S)

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
struct node{
    ll k[2][2];
}a,e,t;
int n,k,b;
ll mod;

inline node jc(node a1,node b1)
{
    int i,j,l;
    node c;
    c.k[0][0]=c.k[0][1]=c.k[1][0]=c.k[1][1]=0;
    for(i=0;i<=1;++i)
     for(j=0;j<=1;++j) 
      for(l=0;l<=1;++l)
       c.k[i][j]=(c.k[i][j]+a1.k[i][l]*b1.k[l][j]%mod)%mod;
    return c;
}
inline node add(node a1,node b1)
{
    int i,j;
    node c;
    for(i=0;i<=1;++i)
     for(j=0;j<=1;++j)
      c.k[i][j]=(a1.k[i][j]+b1.k[i][j])%mod;
    return c;
}
node poww(node x,int p)
{
    node sum;
    sum.k[0][0]=sum.k[1][1]=1; sum.k[0][1]=sum.k[1][0]=0;
    node c=x;
    while(p)
    {
        if (p&1) sum=jc(sum,c);
        p>>=1;
        c=jc(c,c);
    }
    return sum;
}
node check(int h)
{
    if(h==1) return t;
    node sum=poww(t,((h+1)/2));
    node s1=check(h/2);
    if(h%2) return add(t,jc(add(t,sum),s1));
     else return jc(add(e,sum),s1);
}


int main()
{
    a.k[0][0]=a.k[0][1]=a.k[1][0]=1; a.k[1][1]=0;
    e.k[0][0]=e.k[1][1]=1; e.k[0][1]=e.k[1][0]=0;
    while((scanf("%d%d%d%lld",&k,&b,&n,&mod))==4)
     {
        node t1=poww(a,b);
        t=poww(a,k);
        node ans=jc(t1,add(e,check(n-1)));
        printf("%lld\n",ans.k[0][1]);
     }
}
版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/reverie_mjp

相关文章推荐

HDU1588 Gauss Fibonacci (矩阵快速幂+等比数列二分求和)

Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a ve...

hdu1588 Gauss Fibonacci(矩阵快速幂+二分求矩阵等比和)

题目: Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/...

[HDU1588]Gauss Fibonacci(递推+矩阵快速幂)

不觉碧山暮,但闻万壑松。

hdu1588 Gauss Fibonacci (矩阵快速幂)

P.S. 感谢大神,借鉴于此:http://blog.csdn.net/shiyuankongbu/article/details/8458459  讲的真心很明白,,,        ...

HDU 1588 Gauss Fibonacci(矩阵快速幂)

题目地址:HDU 1588 用于构造斐波那契的矩阵为 1,1 1,0 设这个矩阵为A。 sum=f(b)+f(k+b)+f(2*k+b)+f(3*k+b)+........+f((n-1)*...

【HDU】1588 Gauss Fibonacci 矩阵快速幂

传送门:【HDU】1588 Gauss Fibonacci

[POJ 1588] Gauss Fibonacci (矩阵快速幂)

链接HDU 1588题意求若干项fibonacci数列的sum(fibonacci[j]),j = k*i + b。 fibonacci数列的等差数列项求和。题解矩阵快速幂的一个经典例题就是快速求出...

hdu 1588 (Fibonacci+二分+矩阵快速幂)

点击打开链接 g[i]=i*k+b f[0]=0,f[1]=1 f[n]=f[n-1]+f[n-2]; 求sum(f[g[i]])%m,i>=0&&i<n sum=f[b]+f[k+b]+..+f...

hdu 1588 Gauss Fibonacci(矩阵乘法,二分)

题目分析:[f(n),f(n-1)]=[1,1;  1,0]^(n-1)*[f(1),f(0)]; A=[1,1;1,0]; 求:{A^b*(A^0+A^k+A^[2*k]+A^[3*k],,,+...

hdu 1588 Gauss Fibonacci(矩阵乘法+二分)

题目分析:构造矩阵a={{0,1},{1,1}},{y,x+y}=a*{x,y}; {ans, --}=求和(0->n-1){(a^k)^i}*a^b*{0,1}....... 正确代码: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)