码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5667 Sequence【矩阵快速幂】

时间:2016-04-17 23:09:35      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 285    Accepted Submission(s): 92


Problem Description
    技术分享Holion August will eat every thing he has found.

    技术分享Now there are many foods,but he does not want to eat all of them at once,so he find a sequence.

f技术分享n技术分享=?技术分享?技术分享?技术分享?技术分享?技术分享1,技术分享a技术分享b技术分享,技术分享a技术分享b技术分享f技术分享c技术分享n?1技术分享f技术分享n?2技术分享,技术分享技术分享n=1技术分享n=2技术分享otherwise技术分享技术分享技术分享

    技术分享He gives you 5 numbers n,a,b,c,p,and he will eat f技术分享n技术分享技术分享 foods.But there are only p foods,so you should tell him f技术分享n技术分享技术分享 mod p.
 

Input
    技术分享The first line has a number,T,means testcase.

    技术分享Each testcase has 5 numbers,including n,a,b,c,p in a line.

    1T10,1n10技术分享18技术分享,1a,b,c10技术分享9技术分享技术分享,p技术分享 is a prime number,and p10技术分享9技术分享+7技术分享.
 

Output
    技术分享Output one number for each case,which is f技术分享n技术分享技术分享 mod p.
 Sample Input
1
5 3 3 3 233
 


Sample Output
190



题目大意:根据给出的公式,求其解fn、

思路:根据公式得出结论,求的的fn,一定是a的多少次方,所以我们锁定思路是求a的幂数。然后再用快速幂求出解。

公式不难推出:

Fn=Fn-1*c+Fn-2+b;

然后我们也不难写出矩阵:

技术分享技术分享

坑点:在矩阵快速幂的时候要注意先对mod-1,再进行。也就是要注意amodp==0的情况。

然后再进行a的这些次方即可。

Ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define ll long long int
ll mod;
typedef struct Matrix
{
    ll mat[3][3];
} matrix;
matrix A,B,tmp;
Matrix matrix_mul(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    int i,j,k;
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            for(int k=0; k<3; k++)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                c.mat[i][j]%=mod;
            }
        }
    }
    return c;
}
Matrix matrix_quick_power(matrix a,ll k)//矩阵快速幂0.0
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0; i<3; i++)
        b.mat[i][i]=1;//单位矩阵b
    while(k)
    {
        if(k%2==1)
        {
            b=matrix_mul(a,b);
            k-=1;
        }
        else
        {
            a=matrix_mul(a,a);
            k/=2;
        }
    }
    return b;
}
ll Mod_pow(ll a, ll b, ll p)
{
    a %= p;
    ll ans = 1ll;
    while (b)
    {
        if (b & 1)ans = ans*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return ans;
}
int main()
{
    ll n,a,b,c;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%I64d%I64d%I64d", &n, &a, &b, &c, &mod);
        A.mat[0][0] = c, A.mat[0][1] = 1, A.mat[0][2] = b;
        A.mat[1][0] = 1, A.mat[1][1] = 0, A.mat[1][2] = 0;
        A.mat[2][0] = 0, A.mat[2][1] = 0, A.mat[2][2] = 1;
        if (n == 1)printf("1\n");
        else if (n == 2)printf("%I64d\n", Mod_pow(a, b, mod));
        else
        {
            mod--;
            B =  matrix_quick_power(A,(n - 2));
            ll tmp=B.mat[0][0]*b+B.mat[0][2];
            mod++;
            ll ans = Mod_pow(a, tmp, mod);
            printf("%I64d\n",ans);
        }
    }
}











hdu 5667 Sequence【矩阵快速幂】

标签:

原文地址:http://blog.csdn.net/mengxiang000000/article/details/51171043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!