由于模数不一定是大质数,我们不能直接套等比数列求和公式。
换一种思路,数列 \(\langle1,A,A^2,\cdots,A^{X-1}\rangle\) 可以看做线性递推,因此设计矩阵:
\[\boldsymbol T= \begin{bmatrix} A & 0 \\ 1 & 1 \\ \end{bmatrix} \]显然有:
\[\begin{bmatrix} A^i \\ S_{i-1} \\ \end{bmatrix} \boldsymbol T = \begin{bmatrix} A^{i+1}\\ S_i \\ \end{bmatrix} \]因此 \(\boldsymbol T^X\) 的左下角元素即为答案。
时间复杂度 \(\mathcal O(\log X)\)。
// Problem: E - Geometric Progression
// Contest: AtCoder - AtCoder Beginner Contest 293
// URL: https://atcoder.jp/contests/abc293/tasks/abc293_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(ll x=(y);x<=(z);x++)
#define per(x,y,z) for(ll x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
#define likely(exp) __builtin_expect(!!(exp), 1)
#define unlikely(exp) __builtin_expect(!!(exp), 0)
using namespace std;
typedef long long ll;
mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
ll randint(ll L, ll R) {
uniform_int_distribution<ll> dist(L, R);
return dist(rnd);
}
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
ll a, x, m;
struct Matrix {
ll a[2][2];
Matrix() {memset(a, 0, sizeof(a));}
friend Matrix operator*(const Matrix& a, const Matrix& b) {
Matrix c;
rep(i, 0, 1) rep(j, 0, 1) rep(k, 0, 1) (c.a[i][j] += a.a[i][k] * b.a[k][j] % m) %= m;
return c;
}
friend Matrix operator^(Matrix a, ll k) {
Matrix c;
c.a[0][0] = c.a[1][1] = 1;
for(; k; k >>= 1, a = a * a) if(k & 1) c = c * a;
return c;
}
}mat;
/*
u * a 0 = a*u
s 1 1 s+u
*/
int main() {
scanf("%lld%lld%lld", &a, &x, &m);
mat.a[0][0] = a % m;
mat.a[1][0] = mat.a[1][1] = 1;
mat = mat ^ x;
printf("%lld\n", mat.a[1][0]);
return 0;
}
标签:return,Matrix,题解,ll,bmatrix,ABC293E,Geometric,define,mat
From: https://www.cnblogs.com/ruierqwq/p/abc293e.html