【Henu ACM Round#24 D】Iterated Linear Function

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


把B提取出来就是一个等比数列了。
求和一下会发现是这种形式。
\(B*\frac{(A^n-1)}{A-1}+A^n*x\)
则求一下乘法逆元
写个快速幂就好
A-1的逆元就是\((A-1)^{MOD-2}\)

要注意A=1的情况。
然后n最大可能为10^18
所以乘的时候要先对其取模
不然会乘爆

【代码】

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const LL MOD = 1e9 + 7;

LL A,B,n,x;

LL Pow(LL x,LL y){
    LL temp = 1;
    while (y){
        if (y&1) temp = (temp*x)%MOD;
        x = (x*x)%MOD;
        y>>=1;
    }
    return temp;
}

int main()
{
    cin >> A >> B >> n >> x;
    if (A==1){
        cout<<(x + (n%MOD*B%MOD))%MOD;
    }else{
        LL ni = Pow(A-1,MOD-2);
        LL A_n = Pow(A,n);
        LL temp1 = B*ni%MOD*(A_n-1)%MOD;
        temp1 = (temp1+MOD)%MOD;
        temp1 = (temp1 + A_n*x%MOD)%MOD;
        cout<<temp1<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值