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. const int MAX_SUM = 9 * 55;
  38. int dp[N][MAX_SUM][MAX_SUM];
  39. bool isPrime[MAX_SUM];
  40. vector<int> digit;
  41. string L, R;
  42.  
  43. int DP(int pos, int sumOdd, int sumEven, bool smaller) {
  44. if (pos < 0) return (sumOdd >= sumEven && isPrime[sumOdd - sumEven]);
  45. int ret = dp[pos][sumOdd][sumEven];
  46. if (smaller && ~ret) return ret;
  47. ret = 0;
  48. int limit = (smaller ? 9 : digit[pos]);
  49. FOR(i, 0, limit)
  50. ret = (ret + DP(pos - 1, sumOdd + (pos & 1) * i, sumEven + (pos % 2 == 0) * i, smaller | (i < limit))) % MOD;
  51.  
  52. if (smaller) dp[pos][sumOdd][sumEven] = ret;
  53. return ret;
  54. }
  55.  
  56. void sieve() {
  57. memset(isPrime, true, sizeof isPrime);
  58. isPrime[0] = isPrime[1] = false;
  59. for(int i = 2; i * i < MAX_SUM; i++)
  60. if (isPrime[i])
  61. for(int j = i * i; j < MAX_SUM; j += i)
  62. isPrime[j] = false;
  63. }
  64.  
  65. bool check(string &x) {
  66. int sum = 0;
  67. REP(i, (int)x.size()) {
  68. if (i & 1) sum += (x[i] - '0');
  69. else sum -= (x[i] - '0');
  70. }
  71. return (sum > 0 && isPrime[sum]);
  72. }
  73.  
  74. int f(string &x) {
  75. digit.clear();
  76. for(char c : x) digit.pb(c - '0');
  77. reverse(all(digit));
  78. return DP((int)digit.size() - 1, 0, 0, 0);
  79. }
  80.  
  81. void init(void) {
  82. cin >> L >> R;
  83. }
  84.  
  85. void process(void) {
  86. sieve();
  87. cout << (f(R) - f(L) + check(L) + MOD) % MOD;
  88. }
  89.  
  90. int main() {
  91. ios_base::sync_with_stdio(0);
  92. cin.tie(0); cout.tie(0);
  93. if (fopen(task".inp", "r")) {
  94. freopen(task".inp", "r", stdin);
  95. freopen(task".out", "w", stdout);
  96. }
  97. int tc = 1;
  98. // cin >> tc;
  99. memset(dp, -1, sizeof dp);
  100. while(tc--) {
  101. init();
  102. process();
  103. }
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0.02s 104044KB
stdin
Standard input is empty
stdout
Standard output is empty