1 //WikiOI 1281 Xn数列
2 #include <iostream>
3 using namespace std;
4 typedef long long LL;
5 LL m, a, c, x0, n, MOD;
6 LL mlti(LL a, LL b) //倍增取模乘法
7 {
8 a %= m;
9 LL ans = 0;
10 while(b)
11 {
12 if(b & 1)ans = (ans + a)% m;
13 a = (a << 1)% m;
14 b >>= 1;
15 }
16 return ans;
17 }
18 struct Matrix2
19 {
20 LL val[2][2];
21 Matrix2(LL k1,LL k2,LL k3,LL k4)
22 {
23 val[0][0] = k1; val[0][1] = k2; val[1][0] = k3; val[1][1] = k4;
24 }
25 void Mlti(Matrix2 &m) //矩阵乘法
26 {
27 LL v1 = mlti(val[0][0],m.val[0][0])+mlti(val[0][1],m.val[1][0]);
28 LL v2 = mlti(val[0][0],m.val[0][1])+mlti(val[0][1],m.val[1][1]);
29 LL v3 = mlti(val[1][0],m.val[0][0])+mlti(val[1][1],m.val[1][0]);
30 LL v4 = mlti(val[1][0],m.val[0][1])+mlti(val[1][1],m.val[1][1]);
31 val[0][0] = v1,val[0][1] = v2,val[1][0] = v3,val[1][1] = v4;
32 }
33 };
34
35 int main()
36 {
37 ios::sync_with_stdio(0); //感谢陈思学长!
38 cin >>m >>a >>c >>x0 >>n >>MOD;
39 Matrix2 M(a, 0, 1, 1);
40 Matrix2 ans = M;
41 n -= 1;
42 while(n)
43 {
44 if(n & 1) ans.Mlti(M);
45 M.Mlti(M);
46 n >>=1;
47 }
48 cout << ((mlti(x0, ans.val[0][0])+mlti(c, ans.val[1][0]))%m)%MOD;
49 return 0;
50 }