fork download
  1. // ~~ icebear love attttttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define mp make_pair
  27. #define pb push_back
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define task "icebearat"
  32.  
  33. const int MOD = 1e9 + 7;
  34. const int inf = 1e9 + 27092008;
  35. const ll INF = 1e18 + 27092008;
  36. const int N = 100 + 5;
  37. int k, p, dp[N][N];
  38. vector<int> digit;
  39. string L, R;
  40.  
  41. int DP(int pos, int cnt, bool start, bool smaller) {
  42. if (cnt > k) return 0;
  43. if (pos < 0) return cnt <= k;
  44. int ret = dp[pos][cnt];
  45. if (smaller && ~ret) return ret;
  46. ret = 0;
  47. int limit = (smaller ? 9 : digit[pos]);
  48. FOR(i, 0, limit) {
  49. bool newStart = (start | (i > 0));
  50. int newCnt = cnt + (i == p && newStart);
  51. ret = (ret + DP(pos - 1, newCnt, newStart, smaller | (i < limit))) % MOD;
  52. }
  53. if (smaller) dp[pos][cnt] = ret;
  54. return ret;
  55. }
  56.  
  57. bool check(string &x) {
  58. int cnt = 0;
  59. for(char c : x) if (c - '0' == p)
  60. cnt++;
  61. return cnt <= k;
  62. }
  63.  
  64. int f(string &x) {
  65. digit.clear();
  66. for(char c : x) digit.pb(c - '0');
  67. reverse(all(digit));
  68. return DP((int)digit.size() - 1, 0, 0, 0);
  69. }
  70.  
  71. void init(void) {
  72. cin >> L >> R >> k >> p;
  73. }
  74.  
  75. void process(void) {
  76. memset(dp, -1, sizeof dp);
  77. cout << (f(R) - f(L) + check(L) + MOD) % MOD;
  78. }
  79.  
  80. int main() {
  81. ios_base::sync_with_stdio(0);
  82. cin.tie(0); cout.tie(0);
  83. if (fopen(task".inp", "r")) {
  84. freopen(task".inp", "r", stdin);
  85. freopen(task".out", "w", stdout);
  86. }
  87. int tc = 1;
  88. // cin >> tc;
  89. while(tc--) {
  90. init();
  91. process();
  92. }
  93. return 0;
  94. }
  95.  
  96.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
1