hdu5667 Sequence 【矩阵快速幂+欧拉函数降次】

56 篇文章 0 订阅

多写几项就可以发现每一项都是a的次数有关,然后可以构造一个矩阵,求他的快速幂,这个过程中可以用欧拉函数降幂,即中途对p-1取模。
然后还有一个细节,如果a%p==0的话,要特判为0。

/* ***********************************************
Author        :Maltub
Email         :xiang578@foxmail.com
Blog          :htttp://www.xiang578.top
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
const int N=2048;
ll n,a,b,c,p;

struct node
{
    ll mat[3][3];
}A,B;

node operator *(const node &n1,const node &n2)
{
    node c;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            c.mat[i][j]=0;
            for(int k=0;k<3;k++)
            {
                c.mat[i][j]=(c.mat[i][j]+n1.mat[i][k]*n2.mat[k][j]%(p-1))%(p-1);
            }
        }
    }
    return c;
}
node fst(node n1,ll x)
{
    node c;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            c.mat[i][j]=(i==j);
        }
    }
    while(x)
    {
        if(x&1) c=c*n1;
        n1=n1*n1;
        x>>=1;
    }
    return c;
}

ll pow_mod(ll x,ll y)
{
    ll ans=1;
    for(y;y;y>>=1)
    {
        if(y&1) ans=ans*x%p;
        x=x*x%p;
    }
    return ans;
}

int main()
{
    int _;
    scanf("%d",&_);
    while(_--)
    {
        cin>>n>>a>>b>>c>>p;
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        if(a%p==0)
        {
            printf("0\n");
            continue;
        }
        A.mat[0][0]=b;A.mat[1][0]=0;A.mat[2][0]=b;
        B.mat[0][0]=c;B.mat[0][1]=1;B.mat[0][2]=1;
        B.mat[1][0]=1;B.mat[1][1]=0;B.mat[1][2]=0;
        B.mat[2][0]=0;B.mat[2][1]=0;B.mat[2][2]=1;
        B=fst(B,n-2);
        A=B*A;
        cout<<pow_mod(a,A.mat[0][0])<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值