Advertisement
tsypko

Untitled

Jan 31st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/rope>
  3. using namespace std;
  4. using namespace __gnu_cxx;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef unsigned int ui;
  8. typedef long double ld;
  9. typedef pair<ll, ll> ii;
  10. typedef pair<ii, ii> iii;
  11. ll MOD = 1e9 + 7;
  12. const ld E = 1e-7;
  13. #define null NULL
  14. #define ms(x) memset(x, 0, sizeof(x))
  15. #ifndef LOCAL
  16. #define endl "\n"
  17. #endif
  18. #ifndef LONG_LONG_MAX
  19. #define LONG_LONG_MAX LLONG_MAX
  20. #endif
  21. #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  22. #define _read(x) freopen(x, "r", stdin)
  23. #define _write(x) freopen(x, "w", stdout)
  24. #define files(x) _read(x ".in"); _write(x ".out")
  25. #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL")
  26. #define output _write("output.txt")
  27. #define input _read("input.txt")
  28. #define prev time_prev
  29. #define M_PI acos(-1)
  30. #define remove tipa_remove
  31. #define left tipa_left
  32. #define right tipa_right
  33. #define next tipa_next
  34. #define mod % MOD
  35. #define y1 hello_world
  36. unsigned char ccc;
  37. bool _minus = false;
  38. template<typename T>
  39. inline T sqr(T t) {
  40. return (t * t);
  41. }
  42. inline void read(ll &n) {
  43. n = 0;
  44. _minus = false;
  45. while (true) {
  46. ccc = getchar();
  47. if (ccc == ' ' || ccc == '\n')
  48. break;
  49. if (ccc == '-') {
  50. _minus = true;
  51. continue;
  52. }
  53. n = n * 10 + ccc - '0';
  54. }
  55. if (_minus)
  56. n *= -1;
  57. }
  58. inline void read(int &n) {
  59. n = 0;
  60. _minus = false;
  61. while (true) {
  62. ccc = getchar();
  63. if (ccc == ' ' || ccc == '\n')
  64. break;
  65. if (ccc == '-') {
  66. _minus = true;
  67. continue;
  68. }
  69. n = n * 10 + ccc - '0';
  70. }
  71. if (_minus)
  72. n *= -1;
  73. }
  74. char wwww[19];
  75. int kkkk;
  76. inline void write(ll y) {
  77. long long x = y;
  78. kkkk = 0;
  79. if (x < 0) {
  80. putchar('-');
  81. x *= -1;
  82. }
  83. if (!x)
  84. ++kkkk, wwww[kkkk] = '0';
  85. else
  86. while (x) {
  87. ++kkkk;
  88. wwww[kkkk] = char(x % 10 + '0');
  89. x /= 10;
  90. }
  91. for (int i = kkkk; i >= 1; --i)
  92. putchar(wwww[i]);
  93. }
  94.  
  95. #ifdef LOCAL
  96. //#define DEBUG
  97. #endif
  98.  
  99. #ifdef DEBUG
  100. #define dbg if(1)
  101. #else
  102. #define dbg if(0)
  103. #endif
  104.  
  105. const int MAX = 5e3 + 10;
  106.  
  107. ll KEY = 1191;
  108.  
  109. ll dp[MAX][MAX];
  110.  
  111. string s;
  112.  
  113. inline void upd(ll &a, ll b) {
  114. a += b;
  115. if (a >= MOD) {
  116. a -= MOD;
  117. }
  118. }
  119.  
  120. ll _hash[MAX], h[MAX];
  121.  
  122. ll get(int l, int r) {
  123. ll ans = h[r + 1];
  124. ans -= (h[l] * _hash[r - l + 1]) % MOD;
  125. if (ans < 0) {
  126. ans += MOD;
  127. }
  128. return ans;
  129. }
  130.  
  131. bool check_1(int l1, int r1, int l2, int r2) {
  132. if (s[l1] != s[l2]) {
  133. return s[l1] < s[l2];
  134. }
  135. if (get(l1, r1) == get(l2, r2)) {
  136. return false;
  137. }
  138. while (l1 <= r1) {
  139. if (s[l1] != s[l2]) {
  140. return s[l1] < s[l2];
  141. }
  142. l1++, l2++;
  143. }
  144. return false;
  145. }
  146.  
  147. bool check_2(int l1, int r1, int l2, int r2) {
  148. while (l1 <= r1) {
  149. if (s[l1] != s[l2]) {
  150. return s[l1] < s[l2];
  151. }
  152. l1++, l2++;
  153. }
  154. return false;
  155. }
  156.  
  157. bool check(int l1, int r1, int l2, int r2) {
  158. return check_1(l1, r1, l2, r2);
  159. assert(s[l1] != '0');
  160. assert(s[l2] != '0');
  161. assert(r1 - l1 == r2 - l2);
  162. bool res = check_1(l1, r1, l2, r2);
  163. assert(res == check_2(l1, r1, l2, r2));
  164. return res;
  165. }
  166.  
  167. int main() {
  168. sync;
  169. srand(time(NULL));
  170. cout.precision(3);
  171. cout << fixed;
  172. #ifdef LOCAL
  173. input;
  174. #else
  175.  
  176. #endif
  177.  
  178. int n;
  179. cin >> n;
  180. cin >> s;
  181. h[0] = 0;
  182. _hash[0] = 1;
  183. for(int i = 1; i <= n; i++) {
  184. h[i] = (h[i - 1] * KEY + s[i - 1]) % MOD;
  185. _hash[i] = (_hash[i - 1] * KEY) % MOD;
  186. }
  187. dp[0][0] = 1;
  188.  
  189. for(int i = 0; i + 1 < n; i++) {
  190. for(int j = 0; j < n; j++) {
  191. if(dp[i][j]) {
  192. upd(dp[i + 1][j + 1], dp[i][j]);
  193. if(s[i + 1] == '0') {
  194. continue;
  195. }
  196. if(i + j + 1 < n) {
  197. if(check(i - j, i, i + 1, i + j + 1)) {
  198. upd(dp[i + j + 1][j], dp[i][j]);
  199. } else if(i + j + 2 < n) {
  200. upd(dp[i + j + 2][j + 1], dp[i][j]);
  201. }
  202. }
  203. }
  204. }
  205. }
  206.  
  207. ll ans = 0;
  208. for(int j = 0; j < n; j++) {
  209. upd(ans, dp[n - 1][j]);
  210. }
  211. cout << ans << endl;
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement