[ABC293E] Geometric Progression 题解
本题可以使用分治思想。
令
- 当
n 为偶数时,
- 当
n 为奇数时,
上述二式可以递归求解,答案最后加上
注意
放代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int m;
int qpow(int a,int b){
int r=1;
while(b){
if(b&1)r=r%m*a%m;
a=a%m*a%m; b>>=1;
}
return r;
} // 快速幂
int f(int a,int x){
if(!x)return 0; // x=1 时如果不加这行会 RE
if(x==1)return a%m;
if(x&1)return (f(a,x>>1)*(qpow(a,x>>1)+1)%m+qpow(a,x))%m;
else return f(a,x>>1)*(qpow(a,x>>1)+1)%m;
// 套公式
} // f(a,x) 表示解法描述中的 S[x]
main(){
ios::sync_with_stdio(false);
int a,x; cin>>a>>x>>m;
cout<<(f(a,x-1)+1)%m<<endl;
return 0;
}