题解 ABC293E【Geometric Progression】

由于模数不一定是大质数,我们不能直接套等比数列求和公式。

换一种思路,数列 1,A,A2,,AX1 可以看做线性递推,因此设计矩阵:

T=[A011]

显然有:

[AiSi1]T=[Ai+1Si]

因此 TX 的左下角元素即为答案。

时间复杂度 O(logX)

// 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;
}
posted @   rui_er  阅读(164)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示