HDU 5667 Sequence (矩阵快速幂 + 费马小定理)

78 篇文章 0 订阅

Sequence

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



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
BestCoder Round #80

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5667

题目大意:求公式的结果

题目分析:显然取对数,递推式一眼化成矩阵乘法,然后快速幂,因为矩阵快速幂的结果是a的幂,所以矩阵快速幂取模的时候根据费马小定理要模p-1,不过有可能a模p==0,这个时候答案显然是0,但是有可能指数取模的结果是0,这个时候a^0变成1就不对了

#include <cstdio>
#include <cstring>
#define ll long long
ll n, a, b, c, p;
struct matrix  
{  
    ll m[5][5];  
};  
  
matrix multiply(matrix x, matrix y, ll MOD)  
{  
    matrix ans;  
    memset(ans.m, 0, sizeof(ans.m));  
    for(int i = 1; i <= 3; i++)  
        for(int j = 1; j <= 3; j++)  
            if(x.m[i][j])  
                for(int k = 1; k <= 3; k++)  
                    ans.m[i][k] = (ans.m[i][k] + x.m[i][j] * y.m[j][k]) % MOD;  
    return ans;  
}  
  
matrix quickmod(matrix a, ll x, ll MOD)  
{  
    matrix ans;  
    memset(ans.m, 0, sizeof(ans.m));  
    for(int i = 1; i <= 3; i++)  
        ans.m[i][i] = 1;  
    while(x)  
    {  
        if(x & 1)  
            ans = multiply(ans, a, p - 1);  
        x >>= 1;  
        a = multiply(a, a, p - 1);  
    }  
    return ans;  
}  

ll qpow(ll x, ll nn)
{
    ll res = 1;
    while(nn)
    {
        if(nn & 1)
            res = (res * x) % p;
        x = (x * x) % p;
        nn >>= 1;
    }
    return res;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        scanf("%I64d %I64d %I64d %I64d %I64d", &n, &a, &b, &c, &p);
        if(n == 1)
            printf("1\n");
        else if(n == 2)
            printf("%I64d\n", qpow(a, b));
        else
        {
            if(a % p == 0)
                printf("0\n");
            else
            {
                matrix ans;
                ans.m[1][1] = (b * c + b) % (p - 1);
                ans.m[1][2] = b;
                ans.m[1][3] = 1;
                matrix mx;
                mx.m[1][1] = c; mx.m[1][2] = 1; mx.m[1][3] = 0;
                mx.m[2][1] = 1; mx.m[2][2] = 0; mx.m[2][3] = 0;
                mx.m[3][1] = b; mx.m[3][2] = 0; mx.m[3][3] = 1;
                ans = multiply(ans, quickmod(mx, n - 3, p - 1), p - 1);
                printf("%I64d\n", qpow(a, ans.m[1][1]));
            }
        }
    }
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值