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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <bits/stdc++.h>
const int MAXK = 10; const int MOD = 1e9 + 7;
using uint = unsigned int; using ull = unsigned long long;
inline int add(int x, int v) { return x + v >= MOD ? x + v - MOD : x + v; }
inline int dec(int x, int v) { return x - v < 0 ? x - v + MOD : x - v; }
int C[MAXK + 3][MAXK + 3], B[MAXK + 1], inv[MAXK + 3];
int P[MAXK + 1][MAXK + 3];
inline int get(int k, int n) { int ret = 0; for (int i = 0; i < k + 2; i++) ret = ((ull)ret * n + P[k][i]) % MOD; return (ull)ret * inv[k + 1] % MOD; }
inline void init() { inv[0] = inv[1] = 1; for (int i = 2; i <= MAXK + 1; i++) inv[i] = (MOD - MOD / i) * (ull)inv[MOD % i] % MOD; for (int i = 0; i <= MAXK + 1; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]); } B[0] = 1; for (int i = 1, s; i <= MAXK; i++) { s = 0; for (int j = 0; j < i; j++) s = (s + (ull)C[i + 1][j] * B[j]) % MOD; B[i] = (ull)(MOD - s) * inv[i + 1] % MOD; } for (int i = 0; i <= MAXK; i++) { for (int j = 0; j <= i; j++) { P[i][j] = (j & 1) ? ((ull)(MOD - C[i + 1][j]) * B[j] % MOD) : ((ull)C[i + 1][j] * B[j] % MOD); } P[i][i + 1] = 0; } P[0][1] = 1; }
inline int classEuclid(int n, int a, int b, int c, int k1, int k2) { using Info = std::tuple<int, int, int, int>; static std::vector<Info> st; st.clear(); st.reserve(std::__lg(std::min(a, c)) + 3); for (;;) { st.emplace_back(n, a, b, c); if (n < 0 || a == 0) break; if (a >= c) { a %= c; } else if (b >= c) { b %= c; } else { n = ((long long)a * n + b) / c - 1; b = c - b - 1; std::swap(a, c); } }
const int S = k1 + k2; static int buc[2][MAXK + 1][MAXK + 1]; using Ptr = int(*)[MAXK + 1]; Ptr f = buc[0], g = buc[1]; while (!st.empty()) { std::tie(n, a, b, c) = st.back(); st.pop_back(); if (n < 0) { continue; } else if (a == 0) { int q = b / c; for (int i = 0, s; i <= S; i++) { s = get(i, n); for (int j = 0; j <= S - i; j++) { g[i][j] = s; s = (ull)s * q % MOD; } } } else if (a >= c || b >= c) { int q = (a >= c) ? a / c : b / c, d = (a >= c) ? 1 : 0; for (int k1 = 0; k1 <= S; k1++) { for (int k2 = 0, s; k2 <= S - k1; k2++) { s = 0; for (int i = 0, p = 1; i <= k2; i++) { s = (s + (ull)p * C[k2][i] % MOD * f[k1 + i * d][k2 - i]) % MOD; p = (ull)p * q % MOD; } g[k1][k2] = s % MOD; } } } else { static int h[MAXK + 1][MAXK + 1]; for (int k2 = 0; k2 <= S - 1; k2++) { for (int k1 = 0, s; k1 <= S - k2 - 1; k1++) { s = 0; for (int j = 0; j <= k1 + 1; ++j) { s = (s + (ull)P[k1][k1 + 1 - j] * f[k2][j]) % MOD; } h[k1][k2] = (ull)s * inv[k1 + 1] % MOD; } } const int m = ((ull)a * n + b) / c; for (int k1 = 0; k1 <= S; k1++) { int p = get(k1, n); for (int k2 = 0, t; k2 <= S - k1; k2++) { t = 0; for (int i = 0; i < k2; i++) t = (t + (ull)h[k1][i] * C[k2][i]) % MOD; g[k1][k2] = dec(p, t); p = (ull)p * m % MOD; } } } std::swap(f, g); } return f[k1][k2]; }
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T, n, a, b, c, k1, k2; init(); for (std::cin >> T; T--;) { std::cin >> n >> a >> b >> c >> k1 >> k2; std::cout << classEuclid(n, a, b, c, k1, k2) << '\n'; } return 0; }
|