Advertisement
Nelofus

P2371 / 墨墨的等式

Jul 16th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. // Code by Heratino & Nelofus
  2. // 彗星になれなかった姿を
  3. // 笑い飛ばしてほしかった
  4.  
  5. #include <bits/stdc++.h>
  6. using i64 = long long;
  7. using f64 = double;
  8.  
  9. template<typename Ta, typename Tb>
  10. inline void chkmax(Ta &a, const Tb &b) {if (a < b)  a = b;}
  11. template<typename Ta, typename Tb>
  12. inline void chkmin(Ta &a, const Tb &b) {if (a > b)  a = b;}
  13.  
  14. constexpr int N = 5e5 + 10;
  15. i64 f[N];
  16. int n;
  17. i64 l, r;
  18.  
  19. int main() {
  20.     std::cin >> n >> l >> r;
  21.     std::vector<int> a(n);
  22.     for (int &x : a)    std::cin >> x;
  23.  
  24.     memset(f, 0x3f, sizeof(f));
  25.     f[0] = 0;
  26.     int m = a[0];
  27.  
  28.     for (int i = 1; i < n; i++) {
  29.         for (int k = 0; k < std::__gcd(a[i], m); k++) {
  30.             for (int cur = k, c = 0; c < 2; c += (cur == k)) {
  31.                 int nxt = (cur + a[i]) % m;
  32.                 chkmin(f[nxt], f[cur] + a[i]);
  33.                 cur = nxt;
  34.             }
  35.         }
  36.     }
  37.  
  38.     i64 ans = 0;
  39.     auto ct = [&](i64 x) {return x >= 0 ? x / m + 1 : 0;};
  40.     for (int i = 0; i < m; i++) {
  41.         ans += ct(r - f[i]) - ct(l - f[i] - 1);
  42.     }
  43.     std::cout << ans << '\n';
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement