AhmedAshraff

Untitled

Jul 15th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. #define boAshraf { ios_base::sync_with_stdio(false); cin.tie(NULL); }
  4. #define ll long long
  5. #define sz(s) (int)(s).size()
  6. #define endl "\n"
  7. #define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. using namespace __gnu_pbds;
  11. using namespace std;
  12. void File();
  13. void sol();
  14. const int mod = 1e9 + 7, N = 1e5 + 5;
  15. int add(ll a, ll b) {
  16.     return (0ll + a%mod + b%mod) % mod;
  17. }
  18. int mul(ll a, ll b) {
  19.     return (1LL * a%mod * b%mod) % mod;
  20. }
  21. ll a[N];
  22. int dp1[N][2], dp2[N][3];
  23. ll k;
  24. int rec1(int i, int curr) {
  25.     if (i == 0) return curr;
  26.     int &ret = dp1[i][curr];
  27.     if (~ret) return ret;
  28.     ret = 0;
  29.     if (curr) {
  30.         ret = add(ret, mul(k - 2, rec1(i - 1, curr)));
  31.         ret = add(ret, rec1(i - 1, 0));
  32.     } else {
  33.         ret = add(ret, mul(k - 1, rec1(i - 1, 1)));
  34.     }
  35.     return ret;
  36. }
  37. int rec2(int i, int curr) {
  38.     if (i == 0) return curr != 1;
  39.     int &ret = dp2[i][curr];
  40.     if (~ret) return ret;
  41.     ret = 0;
  42.     if (curr == 0) {
  43.         ret = add(ret, rec2(i - 1, 1));
  44.         ret = add(ret, mul(k - 2, rec2(i - 1, 2)));
  45.     } else if (curr == 1) {
  46.         ret = add(ret, rec2(i - 1, 0));
  47.         ret = add(ret, mul(k - 2, rec2(i - 1, 2)));
  48.     } else {
  49.         ret = add(ret, rec2(i - 1, 0));
  50.         ret = add(ret, rec2(i - 1, 1));
  51.         ret = add(ret, mul(max(0LL,k - 3), rec2(i - 1, 2)));
  52.     }
  53.     return ret;
  54. }
  55. int main() {
  56.     boAshraf
  57.     File();
  58.     int t = 1;
  59.     //cin >> t;
  60.     while (t--) {
  61.         sol();
  62.     }
  63.     return 0;
  64. }
  65. void sol() {
  66.     int n;
  67.     ll l, r;
  68.     cin >> n >> l >> r;
  69.     memset(dp1, -1, sizeof dp1);
  70.     memset(dp2, -1, sizeof dp2);
  71.     for (int i = 0; i < n; ++i) {
  72.         cin >> a[i];
  73.     }
  74.     if (r - l + 1 <= 1) return void (cout << 0);
  75.     k = (r - l + 1);
  76.     int ans = 1;
  77.     int last = 0;
  78.     for (int i = 1; i < n; ++i) {
  79.         if (a[i] == -1) continue;
  80.         if (a[i] == a[last]) {
  81.             ans = mul(ans, rec1(i-last - 1, 0));
  82.         } else {
  83.             ans = mul(ans, rec2(i-last - 1, 0));
  84.         }
  85.         last = i;
  86.     }
  87.     cout << ans << endl;
  88. }
  89.  
  90. void File() {
  91. #ifndef ONLINE_JUDGE
  92.     freopen("input.txt", "r", stdin);
  93.     freopen("output.txt", "w", stdout);
  94. #endif
  95. }
Advertisement
Add Comment
Please, Sign In to add comment