1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| #include <bits/stdc++.h>
using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define all(a) (a).begin(),(a).end() #define pb(x) push_back(x) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define caseT int _T;scanf("%d",&_T);for (int q=1; q<=_T; ++q) typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const LL mod = 1e9 + 9; const int N = 1e5 + 7; char s[N]; struct Mat { LL A[2][2]; void O() { A[0][0] = A[0][1] = A[1][0] = A[1][1] = 0; } void I() { A[0][0] = A[1][1] = 1; A[0][1] = A[1][0] = 0; } Mat operator *(Mat b) { Mat c; c.O(); for (int i = 0; i < 2; ++i) { for (int k = 0; k < 2; ++k) { for (int j = 0; j < 2; ++j) { c.A[i][j] = (c.A[i][j] + A[i][k] * b.A[k][j]) % mod; } } } return c; } friend Mat operator^(Mat a, LL b) { Mat r; r.I(); while (b) { if (b & 1) r = r * a; a = a * a; b >>= 1; } return r; } Mat() {} Mat(initializer_list<LL> v) { register int p = 0; for (auto &x : v) { A[p / 2][p & 1] = x; ++p; } } }; inline LL qpow(LL a, LL b) { if (a < 0) a = (a % mod + mod) % mod; if (b < 0) b = (b % mod + mod) % mod; LL r = 1; while (b) { if (b & 1) r = r * a % mod; a = a * a % mod; b >>= 1; } return r; } inline LL mul(LL a, LL b) { if (a < 0) a = (a % mod + mod) % mod; if (b < 0) b = (b % mod + mod) % mod; LL r = 0; while (b) { if (b & 1) r = (r + a) % mod; a = (a + a) % mod; b >>= 1; } return r; } LL inv(LL x) { if (x < 0) x = (x % mod + mod) % mod; return qpow(x, mod - 2); } int main(void) { int n, i, k; LL a, b; scanf("%d%I64d%I64d%d", &n, &a, &b, &k); scanf("%s", s); LL a1 = 0; LL inv_a = inv(a); LL fa = qpow(a, n), fb = 1; for (i = 0; i < k; ++i) { a1 = (a1 + (s[i] == '+' ? 1LL : -1LL) * fa % mod * fb % mod) % mod; fa = fa * inv_a % mod; fb = fb * b % mod; } Mat A = { a1, a1, 0LL, 0LL }; Mat B = { 1LL, 1LL, 0LL, qpow(inv_a, k)*qpow(b, k) % mod }; B = B ^ ((n + 1) / k - 1); A = A * B; printf("%I64d\n", ((A.A[0][1] % mod) + mod) % mod); return 0; }
|