Advertisement
tien_noob

sigma (a * i + b)/c, i = 0->n

Oct 24th, 2022 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pii pair<int, int>
  3. #define fi first
  4. #define se second
  5. #define ll long long
  6. using namespace std;
  7. const int N = 2e5 + 5;
  8. vector<int> gr[N];
  9. pii p[N];
  10. ll ans;
  11. /*
  12. f(a, b, c, n) = (a*i+b)/c, 0 <= i <= n
  13. a < n, b < n
  14. k = (a*n+b)/c
  15. for(i, 0, n)
  16.     for(j, 0, k-1)ans += (j < (a*i+b)/c)
  17. for(j, 0, k-1)
  18.     for(i, 0, n)ans += (j < (a*i+b)/c)
  19. for(j, 0, k-1)
  20.     for(i, 0, n)ans += (j*c+c <= a*i+b)
  21. for(j, 0, k-1)
  22.     for(i, 0, n)ans += ((j*c+c-b-1)/a < i)
  23. for(j, 0, k-1)
  24.     ans += n-(j*c+c-b - 1)/a;
  25. = n*k-f(c, c-b-1 , a, k-1)
  26. */
  27. ll f(ll a, ll b, ll c, ll n)
  28. {
  29.  
  30.     if (a == 0)
  31.         return (n + 1) * (b / c);
  32.     if (a >= c || b >= c)
  33.         return f(a % c, b % c, c, n) + n * (n + 1) / 2 * (a / c) + (n + 1) * (b / c);
  34.     ll k = (a * n + b) / c;
  35.     return n * k - f(c, c - b - 1, a, k - 1);
  36. }
  37. int main()
  38. {
  39.     ios_base::sync_with_stdio(0),
  40.         cin.tie(0);
  41.     int t;
  42.     cin >> t;
  43.     while (t-- > 0)
  44.     {
  45.         ll a, b, c, d;
  46.         cin >> a >> b >> c >> d;
  47.         int k = (d - 2) / (c - b);
  48.         ans = k - f(c, a, d, k) + f(b, a - 1, d, k);
  49.         cout << ans << '\n';
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement