From 07d3deb8e2e81ac75fdaf5ec6e8ab27607b5a473 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 11 Oct 2022 16:26:23 +0800 Subject: [PATCH] =?UTF-8?q?P5431=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E4=B9=98=E6=B3=95=E9=80=86=E5=85=83=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/89461048 --- Luogu/P5431/P5431.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Luogu/P5431/P5431.cpp diff --git a/Luogu/P5431/P5431.cpp b/Luogu/P5431/P5431.cpp new file mode 100644 index 00000000..a3243a8d --- /dev/null +++ b/Luogu/P5431/P5431.cpp @@ -0,0 +1,53 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e6 + 5; + +int n, p, _k, k, a[N], b[N], s[N], ans; + +int binpow(int a, int b) { + int res = 1; + + while (b) { + if (b & 1) res = static_cast(res) * a % p; + a = static_cast(a) * a % p; + b >>= 1; + } + + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> p >> _k; + + s[0] = 1; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + s[i] = static_cast(s[i - 1]) * a[i] % p; + } + + b[n + 1] = binpow(s[n], p - 2); + + for (int i = n; i; i--) { + b[i] = static_cast(b[i + 1]) * a[i] % p; + } + + k = _k; + + for (int i = 1; i <= n; i++) { + ans = (ans + static_cast(s[i - 1]) * b[i + 1] % p * k) % p; + k = static_cast(k) * _k % p; + } + + cout << ans << endl; + + return 0; +}