diff options
| author | Lucas Schwebler <lucas.schwebler@gmail.com> | 2024-09-07 22:02:34 +0200 |
|---|---|---|
| committer | Lucas Schwebler <lucas.schwebler@gmail.com> | 2024-09-07 22:02:34 +0200 |
| commit | 6ae1d00fca78145c73f826c5490452726b9bf161 (patch) | |
| tree | 591921b79e3c3747906dee44a5067ae9a3ca5701 /content/math/divSum.cpp | |
| parent | e1b35ad44b9168cfd1a6d4ca4ed7658b81e93663 (diff) | |
add divSum (sum of floor linear)
Diffstat (limited to 'content/math/divSum.cpp')
| -rw-r--r-- | content/math/divSum.cpp | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/content/math/divSum.cpp b/content/math/divSum.cpp new file mode 100644 index 0000000..dc4bc4d --- /dev/null +++ b/content/math/divSum.cpp @@ -0,0 +1,9 @@ +// Calculates the sum of (a*i+b)/m for i=0..(n-1) in O(log(n)) +// Note that b should not be negative! +ll divSum(ll n, ll m, ll a, ll b){ + if(m == 0) return 0; + ll ans = a/m * n*(n-1) / 2 + b/m * n; + a %= m, b %= m; + ll y = (a*(n-1)+b)/m; + return ans + y*(n-1) - divSum(y, a, m, m-b-1); +}
\ No newline at end of file |
