You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
typedef long long ll, i64;
ll mod;
struct Mat
{
ll a[2][2];
void clear()
{
ll *const p = (ll *)a;
*p = 0;
*(p + 1) = 0;
*(p + 2) = 0;
*(p + 3) = 0;
}
void init()
{
ll *const p = (ll *)a;
*p = 1;
*(p + 1) = 0;
*(p + 2) = 0;
*(p + 3) = 1;
}
};
Mat mat_mul(CRP(Mat, lhs), CRP(Mat, rhs))
{
Mat v;
v.clear();
auto a = lhs.a, b = rhs.a;
auto c = v.a;
c[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % mod;
c[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % mod;
c[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % mod;
c[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % mod;
return v;
}
Mat mat_fpow(Mat a, ll b)
{
Mat r;
r.init();
for (; b; b >>= 1, a = mat_mul(a, a))
if (b & 1)
r = mat_mul(r, a);
return r;
}
Mat mat_fpow_10(Mat a, int *rb, int len)
{
Mat r;
r.init();
for (int i = 0; i < len; i++, a = mat_fpow(a, 10))
r = mat_mul(r, mat_fpow(a, rb[i]));
return r;
}
const int N = 1e6 + 50;
char s[N];
int res[N];
int main()
{
ll x0, x1, a, b;
scanf("%lld%lld%lld%lld%s%lld", &x0, &x1, &a, &b, s, &mod);
int len = strlen(s);
for (int i = 0; i < len; i++) res[len - i - 1] = s[i] - '0';
Mat ini;
ini.a[0][0] = (a * x1 + b * x0) % mod;
ini.a[1][0] = x1;
ini.a[0][1] = x1;
ini.a[1][1] = x0;
Mat fac;
fac.a[0][0] = a;
fac.a[1][0] = b;
fac.a[0][1] = 1;
fac.a[1][1] = 0;
auto re = mat_mul(ini, mat_fpow_10(fac, res, len));
printf("%lld", re.a[1][1]);
return 0;
}