【Henu ACM Round#24 D】Iterated Linear Function
阿新 • • 發佈:2018-04-17
linear contest cti () ace n-1 size std out
【鏈接】 我是鏈接,點我呀:)
【題意】
在這裏輸入題意
【題解】
把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;
}
【Henu ACM Round#24 D】Iterated Linear Function