hdu 5667 Sequence 矩阵快速幂+费马小定理

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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.

fn=⎧⎩⎨⎪⎪1,ab,abfcn1fn2,n=1n=2otherwise

    He gives you 5 numbers n,a,b,c,p,and he will eat fn foods.But there are only p foods,so you should tell him fn 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,1n1018,1a,b,c109,p is a prime number,and p109+7.
 

 

Output
    Output one number for each case,which is fn mod p.
 

 

Sample Input
1 5 3 3 3 233
 

 

Sample Output
190
 

 

Source
思路:就是f(n)=f(n-1)*c+f(n-2)  +b;
   求a^f(n)%p=a^(f(n)%(p-1))%p;//费马小
   矩阵构造博客:http://www.cnblogs.com/frog112111/archive/2013/05/19/3087648.html
   还有个坑点a%p==0
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
ll a,b,c,n,p;
struct is
{
    ll a[10][10];
};
is juzhenmul(is a,is b,ll hang ,ll lie)
{
    int i,t,j;
    is ans;
    memset(ans.a,0,sizeof(ans.a));
    for(i=1;i<=hang;i++)
    for(t=1;t<=lie;t++)
    for(j=1;j<=lie;j++)
    {
        ans.a[i][t]+=(a.a[i][j]*b.a[j][t]);
        ans.a[i][t]%=(p-1);
    }
    return ans;
}
is quickpow(is ans,is a,ll x)
{
    while(x)
    {
        if(x&1)  ans=juzhenmul(ans,a,3,3);
        a=juzhenmul(a,a,3,3);
        x>>=1;
    }
    return ans;
}
ll quickpow1(ll a,ll n)
{
    ll mul=1;
    if(a%p==0)
    return 0;
    while(n)
    {
        if(n&1) mul*=a,mul%=p;
        n>>=1;
        a=a*a;
        a%=p;
    }
    return mul;
}
int main()
{
    is ans,base,gg;
    int gggg;
    scanf("%d",&gggg);
    while(gggg--)
    {
        ll fn;
        scanf("%I64d%I64d%I64d%I64d%I64d",&n,&a,&b,&c,&p);
        if(n>2)
        {
            memset(ans.a,0,sizeof(ans.a));
            ans.a[1][1]=1;
            ans.a[2][2]=1;
            ans.a[3][3]=1;
            memset(base.a,0,sizeof(base.a));
            base.a[1][1]=c;
            base.a[1][2]=1;
            base.a[2][1]=1;
            base.a[3][1]=1;
            base.a[3][3]=1;
            ans=quickpow(ans,base,n-2);
            gg.a[1][1]=b;
            gg.a[1][2]=0;
            gg.a[1][3]=b;
            ans=juzhenmul(gg,ans,1,3);
            fn=ans.a[1][1];
        }
        else
        {
            if(n==1)
            fn=0;
            else
            fn=b;
        }
        printf("%I64d\n",quickpow1(a,fn));
    }
    return 0;
}
View Code

 

 
 
 

转载于:https://www.cnblogs.com/jhz033/p/5426878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值