#pragma GCC OPTIMIZE("O3,unroll-loops") #include <bits/stdc++.h> using namespace std; const int N = 5'007; const int MOD = 998'244'353; int dp[N][2]; int knewt[N][2]; int mem_dp[N][3][2]; int help_dp[2][N][2]; int coef[N][2]; int dp_coef[2][N][N][2]; int rev_nr[N]; int rev_sil[N]; int fast(int a, int b) { int ret = 1; while (b) { if (b & 1) { ret = 1LL * ret * a % MOD; } b >>= 1; a = 1LL * a * a % MOD; } return ret; } int newton(int a, int b) { int ret = 1; for (int i = 0; i < b; ++i) { ret = 1LL * ret * (a - i) % MOD; ret = 1LL * ret * rev_nr[i + 1] % MOD; } return ret; } void get_coefs(int n, int c) { dp_coef[(n + 1) & 1][0][0][0] = 1; dp_coef[(n + 1) & 1][0][0][1] = 1; for (int i = n; i >= 1; --i) { int id = i & 1; int max_take = n / i, prev_max = n / (i + 1); dp_coef[id][0][0][0] = dp_coef[id][0][0][1] = 1; for (int j = 1; j <= max_take && j <= c; ++j) { for (int s = 1; s <= n; ++s) { for (int t = 0; t < 2; ++t) { int cur = 1; __int128_t tmp = j <= prev_max ? dp_coef[id ^ 1][j][s][t] : 0; for (int k = 1; k * i <= s && k <= j; ++k) { cur = 1LL * cur * rev_nr[k] % MOD * knewt[i][t] % MOD; if (j - k <= prev_max) { tmp += 1LL * cur * dp_coef[id ^ 1][j - k][s - k * i][t]; } } dp_coef[id][j][s][t] = tmp % MOD; } } } } for (int i = 1; i <= n; ++i) { for (int t = 0; t < 2; ++t) { coef[i][t] = 0; int cur = 1; for (int j = 1; j <= i && j <= c; ++j) { cur = 1LL * cur * (c - j + 1) % MOD; coef[i][t] = (coef[i][t] + 1LL * cur * dp_coef[1][j][i][t]) % MOD; } } } } void precalc(int n, int c) { dp[1][0] = c; dp[1][1] = 1; help_dp[0][0][0] = 1; mem_dp[0][0][0] = 1; for (int i = 1; i < n; ++i) { int id = i & 1; help_dp[id][0][0] = 1; int max_k = n / i; knewt[0][0] = knewt[0][1] = 1; for (int k = 1; k <= max_k; ++k) { knewt[k][0] = 1LL * knewt[k - 1][0] * (dp[i][0] + k - 1) % MOD * rev_nr[k] % MOD; knewt[k][1] = 1LL * knewt[k - 1][1] * (dp[i][0] + dp[i][1] + k - 1) % MOD * rev_nr[k] % MOD; } get_coefs(max_k, c); for (int j = 1; j <= n; ++j) { help_dp[id][j][0] = help_dp[id ^ 1][j][0]; help_dp[id][j][1] = help_dp[id ^ 1][j][1]; for (int k = 1; k * i <= j; ++k) { int val0 = coef[k][0]; help_dp[id][j][0] = (help_dp[id][j][0] + 1LL * help_dp[id ^ 1][j - k * i][0] * val0) % MOD; int val01 = coef[k][1]; help_dp[id][j][1] = (help_dp[id][j][1] + 1LL * help_dp[id ^ 1][j - k * i][1] * val01) % MOD; help_dp[id][j][1] = (help_dp[id][j][1] + 1LL * help_dp[id ^ 1][j - k * i][0] * (val01 + MOD - val0)) % MOD; } } if (i + i <= n) { for (int off = 0; off < 3; ++off) { for (int t = 0; t < 2; ++t) { mem_dp[i][off][t] = help_dp[id][i + i + off][t]; } } } dp[i + 1][1] = help_dp[id][i][0]; dp[i + 1][0] = (help_dp[id][i + 1][1] + 1LL * c * dp[i + 1][1]) % MOD; } } inline int square(const int x) { return 1LL * x * x % MOD; } int calc(int n, int c) { int mid = (n - 1) / 2; int ans = 1LL * c * (mem_dp[mid][n - 1 - 2 * mid][0] + mem_dp[mid][n - 2 * mid][1]) % MOD; if (n & 1) { mid = (n + 1) / 2; ans = (ans + 1LL * c * newton(dp[mid][1] + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * square(dp[mid][1])) % MOD; } else { int dp_00 = dp[n / 2][0] + MOD - 1LL * c * dp[n / 2][1] % MOD; ans = (ans + 1LL * c * newton(dp_00 + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * square(dp_00)) % MOD; ans = (ans + 1LL * square(c) * dp_00 % MOD * dp[n / 2][1]) % MOD; ans = (ans + 1LL * c * square(c) % MOD * dp_00 % MOD * dp[n / 2][1]) % MOD; ans = (ans + 1LL * square(c) * newton(dp[n / 2][1] + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * c % MOD * square(dp[n / 2][1])) % MOD; int dp_11 = 1LL * c * dp[n / 2][1] % MOD; ans = (ans + 1LL * c * newton(dp_11 + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * square(dp_11)) % MOD; int dp_10 = dp[n / 2 + 1][1] + MOD - 1LL * dp[n / 2][0] * c % MOD; ans = (ans + 1LL * square(c) * dp[n / 2][1] % MOD * dp_10) % MOD; } return ans; } int main() { int cur = rev_nr[0] = 1; for (int i = 1; i < N; ++i) { rev_nr[i] = cur; cur = 1LL * cur * i % MOD; } rev_sil[N - 1] = fast(cur, MOD - 2); for (int i = N - 1; i >= 1; --i) { rev_sil[i - 1] = 1LL * rev_sil[i] * i % MOD; rev_nr[i] = 1LL * rev_nr[i] * rev_sil[i] % MOD; } int l, r, c; scanf("%d %d %d", &l, &r, &c); precalc(r, c); int s = 0; for (int i = l; i <= r; ++i) { int v = calc(i, c); s = (s + v) % MOD; } printf("%d\n", s); return 0; }
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #pragma GCC OPTIMIZE("O3,unroll-loops") #include <bits/stdc++.h> using namespace std; const int N = 5'007; const int MOD = 998'244'353; int dp[N][2]; int knewt[N][2]; int mem_dp[N][3][2]; int help_dp[2][N][2]; int coef[N][2]; int dp_coef[2][N][N][2]; int rev_nr[N]; int rev_sil[N]; int fast(int a, int b) { int ret = 1; while (b) { if (b & 1) { ret = 1LL * ret * a % MOD; } b >>= 1; a = 1LL * a * a % MOD; } return ret; } int newton(int a, int b) { int ret = 1; for (int i = 0; i < b; ++i) { ret = 1LL * ret * (a - i) % MOD; ret = 1LL * ret * rev_nr[i + 1] % MOD; } return ret; } void get_coefs(int n, int c) { dp_coef[(n + 1) & 1][0][0][0] = 1; dp_coef[(n + 1) & 1][0][0][1] = 1; for (int i = n; i >= 1; --i) { int id = i & 1; int max_take = n / i, prev_max = n / (i + 1); dp_coef[id][0][0][0] = dp_coef[id][0][0][1] = 1; for (int j = 1; j <= max_take && j <= c; ++j) { for (int s = 1; s <= n; ++s) { for (int t = 0; t < 2; ++t) { int cur = 1; __int128_t tmp = j <= prev_max ? dp_coef[id ^ 1][j][s][t] : 0; for (int k = 1; k * i <= s && k <= j; ++k) { cur = 1LL * cur * rev_nr[k] % MOD * knewt[i][t] % MOD; if (j - k <= prev_max) { tmp += 1LL * cur * dp_coef[id ^ 1][j - k][s - k * i][t]; } } dp_coef[id][j][s][t] = tmp % MOD; } } } } for (int i = 1; i <= n; ++i) { for (int t = 0; t < 2; ++t) { coef[i][t] = 0; int cur = 1; for (int j = 1; j <= i && j <= c; ++j) { cur = 1LL * cur * (c - j + 1) % MOD; coef[i][t] = (coef[i][t] + 1LL * cur * dp_coef[1][j][i][t]) % MOD; } } } } void precalc(int n, int c) { dp[1][0] = c; dp[1][1] = 1; help_dp[0][0][0] = 1; mem_dp[0][0][0] = 1; for (int i = 1; i < n; ++i) { int id = i & 1; help_dp[id][0][0] = 1; int max_k = n / i; knewt[0][0] = knewt[0][1] = 1; for (int k = 1; k <= max_k; ++k) { knewt[k][0] = 1LL * knewt[k - 1][0] * (dp[i][0] + k - 1) % MOD * rev_nr[k] % MOD; knewt[k][1] = 1LL * knewt[k - 1][1] * (dp[i][0] + dp[i][1] + k - 1) % MOD * rev_nr[k] % MOD; } get_coefs(max_k, c); for (int j = 1; j <= n; ++j) { help_dp[id][j][0] = help_dp[id ^ 1][j][0]; help_dp[id][j][1] = help_dp[id ^ 1][j][1]; for (int k = 1; k * i <= j; ++k) { int val0 = coef[k][0]; help_dp[id][j][0] = (help_dp[id][j][0] + 1LL * help_dp[id ^ 1][j - k * i][0] * val0) % MOD; int val01 = coef[k][1]; help_dp[id][j][1] = (help_dp[id][j][1] + 1LL * help_dp[id ^ 1][j - k * i][1] * val01) % MOD; help_dp[id][j][1] = (help_dp[id][j][1] + 1LL * help_dp[id ^ 1][j - k * i][0] * (val01 + MOD - val0)) % MOD; } } if (i + i <= n) { for (int off = 0; off < 3; ++off) { for (int t = 0; t < 2; ++t) { mem_dp[i][off][t] = help_dp[id][i + i + off][t]; } } } dp[i + 1][1] = help_dp[id][i][0]; dp[i + 1][0] = (help_dp[id][i + 1][1] + 1LL * c * dp[i + 1][1]) % MOD; } } inline int square(const int x) { return 1LL * x * x % MOD; } int calc(int n, int c) { int mid = (n - 1) / 2; int ans = 1LL * c * (mem_dp[mid][n - 1 - 2 * mid][0] + mem_dp[mid][n - 2 * mid][1]) % MOD; if (n & 1) { mid = (n + 1) / 2; ans = (ans + 1LL * c * newton(dp[mid][1] + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * square(dp[mid][1])) % MOD; } else { int dp_00 = dp[n / 2][0] + MOD - 1LL * c * dp[n / 2][1] % MOD; ans = (ans + 1LL * c * newton(dp_00 + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * square(dp_00)) % MOD; ans = (ans + 1LL * square(c) * dp_00 % MOD * dp[n / 2][1]) % MOD; ans = (ans + 1LL * c * square(c) % MOD * dp_00 % MOD * dp[n / 2][1]) % MOD; ans = (ans + 1LL * square(c) * newton(dp[n / 2][1] + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * c % MOD * square(dp[n / 2][1])) % MOD; int dp_11 = 1LL * c * dp[n / 2][1] % MOD; ans = (ans + 1LL * c * newton(dp_11 + 1, 2)) % MOD; ans = (ans + 1LL * newton(c, 2) * square(dp_11)) % MOD; int dp_10 = dp[n / 2 + 1][1] + MOD - 1LL * dp[n / 2][0] * c % MOD; ans = (ans + 1LL * square(c) * dp[n / 2][1] % MOD * dp_10) % MOD; } return ans; } int main() { int cur = rev_nr[0] = 1; for (int i = 1; i < N; ++i) { rev_nr[i] = cur; cur = 1LL * cur * i % MOD; } rev_sil[N - 1] = fast(cur, MOD - 2); for (int i = N - 1; i >= 1; --i) { rev_sil[i - 1] = 1LL * rev_sil[i] * i % MOD; rev_nr[i] = 1LL * rev_nr[i] * rev_sil[i] % MOD; } int l, r, c; scanf("%d %d %d", &l, &r, &c); precalc(r, c); int s = 0; for (int i = l; i <= r; ++i) { int v = calc(i, c); s = (s + v) % MOD; } printf("%d\n", s); return 0; } |