J u's的影响力

链接https://ac.nowcoder.com/acm/contest/3002/J

μ's在九人齐心协力下,影响力越来越大了!

已知第一天影响力为  ,第二天影响力为  ,从第三天开始,每一天的影响力为前两天影响力的乘积再乘以  的  次方。 用数学语言描述是:
设第  天的影响力为  ,那么  ,,对于  , 
她们想知道第  天影响力是多少?
由于这个数可能非常大,只需要输出其对  取模的值就可以了。

输入描述:

一行五个正整数: 。

    

输出描述:

第  天的影响力对  取模的值。

示例1

输入

复制

4 2 3 2 1

输出

复制

72

说明

f(1)=2,f(2)=3,f(3)=f(1)*f(2)*2=12,f(4)=f(2)*f(3)*2=72

备注:

1000000007是素数。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct mt{
    ll a[3][3];
};
mt t(mt a,mt b,ll mod){
    mt res;
    int i,j,k;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            res.a[i][j]=0;
            for(k=0;k<3;k++){
                res.a[i][j]+=a.a[i][k]*b.a[k][j]%mod;
                res.a[i][j]%=mod;
            }
        }
    }
    return res;
}
mt power(mt a,ll b,ll mod){
    mt res;
    int i,j;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            res.a[i][j]=0;
        }
    }
    res.a[0][0]=res.a[1][1]=res.a[2][2]=1;
  
    while(b){
        if(b&1)res=t(res,a,mod);
        b>>=1;
        a=t(a,a,mod);
    }
    return res;
}
ll feb(ll n,ll mod){
    mt temp;
    int i,j;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            temp.a[i][j]=0;
        }
    }
    temp.a[0][1]=temp.a[1][1]=temp.a[1][0]=temp.a[1][2]=1;
    mt res=power(temp,n-1,mod);
    return (res.a[0][0]+res.a[0][1])%mod;
}
ll feb2(ll n,ll mod){
    mt temp;
    int i,j;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            temp.a[i][j]=0;
        }
    }
    temp.a[0][1]=temp.a[1][1]=temp.a[1][0]=temp.a[1][2]=temp.a[2][2]=1;
    mt res=power(temp,n-1,mod);
    return (res.a[0][0]+2*res.a[0][1]+res.a[0][2])%mod;
}
ll power(ll a,ll b,ll mod){
    ll res=1;
  
    while(b){
        if(b&1)res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
int main(){
    int m=1e9+7;
  
    int i,j;
    ll n,x,y,a,b;
    cin>>n>>x>>y>>a>>b;
    if(n==1){cout<<x%m;return 0;}
    if(n==2){cout<<y%m;return 0;}
    if(x%m==0||y%m==0||a%m==0){cout<<0;return 0;}
    x%=m;
    y%=m;
  
    a=power(a%m,b,m);       //这里要注意a对m取模
    ll k3 = (feb(n,m-1)-1+m-1)%(m-1);
    cout<<power(x,feb(n-2,m-1),m)*power(y,feb(n-1,m-1),m)%m*power(a%m,k3,m)%m<<endl;
  
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值