题解 ABC293E【Geometric Progression】
由于模数不一定是大质数,我们不能直接套等比数列求和公式。
换一种思路,数列 可以看做线性递推,因此设计矩阵:
显然有:
因此 的左下角元素即为答案。
时间复杂度 。
// 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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!