Advertisement
DobriyKrot

F

Jan 3rd, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. #define int long long
  8.  
  9. signed main() {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. int t;
  13. cin >> t;
  14. while (t--) {
  15. int s, l, r, k;
  16. cin >> s >> l >> r >> k;
  17. string v = to_string(s);
  18. map<int, int> dp[v.size()][k + 1];
  19. for (int i = 0; i < v.size(); ++i) {
  20. for (int j = i - 1; j >= -1; --j) {
  21. for (int d = 1; d <= k; ++d) {
  22. int cur = stoll(v.substr(j + 1, i - j));
  23. if (v[j + 1] == '0' && i - j > 1) {
  24. continue;
  25. }
  26. if (j == -1) {
  27. dp[i][1][cur] = 1;
  28. continue;
  29. }
  30. for (int p = cur - r; p <= cur - l; ++p) {
  31. if (dp[j][d - 1].find(p) != dp[j][d - 1].end()) {
  32. dp[i][d][cur] += dp[j][d - 1][p];
  33. }
  34. }
  35. for (int p = cur + l; p <= cur + r; ++p) {
  36. if (dp[j][d - 1].find(p) != dp[j][d - 1].end()) {
  37. dp[i][d][cur] += dp[j][d - 1][p];
  38. }
  39. }
  40. }
  41. }
  42. }
  43. int sum = 0;
  44. for (auto i : dp[v.size() - 1][k]) {
  45. sum += i.second;
  46. // cout << i.first << ' ' << i.second << '\n';
  47. }
  48. cout << sum << '\n';
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement