summaryrefslogtreecommitdiff
path: root/content/math/divSum.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'content/math/divSum.cpp')
-rw-r--r--content/math/divSum.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/content/math/divSum.cpp b/content/math/divSum.cpp
new file mode 100644
index 0000000..48302b5
--- /dev/null
+++ b/content/math/divSum.cpp
@@ -0,0 +1,8 @@
+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);
+}