Ayoub thinks that he is a very smart person, so he created a function f(s), where s is a binary string (a string which contains only symbols "0" and "1"). The function f(s) is equal to the number of substrings in the string s that contains at least one symbol, that is equal to "1".
More formally, f(s) is equal to the number of pairs of integers (l, r), such that 1 ≤ l ≤ r ≤ |s| (where |s| is equal to the length of string s), such that at least one of the symbols s_l, s_{l+1}, …, s_r is equal to "1".
For example, if s = "01010" then f(s) = 12, because there are 12 such pairs (l, r): (1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5).
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers n and m and asked him this problem. For all binary strings s of length n which contains exactly m symbols equal to "1", find the maximum value of f(s).
Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers n, m (1 ≤ n ≤ 10^{9}, 0 ≤ m ≤ n) — the length of the string and the number of symbols equal to "1" in it.
Output
For every test case print one integer number — the maximum value of f(s) over all strings s of length n, which has exactly m symbols, equal to "1".
Example
Input
5
3 1
3 2
3 3
4 0
5 2
Output
4
5
6
0
12
Note
In the first test case, there exists only 3 strings of length 3, which has exactly 1 symbol, equal to "1". These strings are: s_1 = "100", s_2 = "010", s_3 = "001". The values of f for them are: f(s_1) = 3, f(s_2) = 4, f(s_3) = 3, so the maximum value is 4 and the answer is 4.
In the second test case, the string s with the maximum value is "101".
In the third test case, the string s with the maximum value is "111".
In the fourth test case, the only string s of length 4, which has exactly 0 symbols, equal to "1" is "0000" and the value of f for that string is 0, so the answer is 0.
In the fifth test case, the string s with the maximum value is "01010" and it is described as an example in the problem statement.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve for maximum possible f(s) where f(s) = number of substrings containing at least one '1'.
Given length n and exactly m ones.
We need to maximize f(s) over all placements of m ones in a binary string of length n.
We can think of complement: total substrings = n*(n+1)/2. f(s) = total substrings - number of substrings with all zeros.
So maximizing f(s) is equivalent to minimizing number of all-zero substrings. That is, minimize the total count of substrings that consist entirely of zeros.
Let the zeros be arranged in blocks. Suppose we have positions of ones, dividing zeros into gaps: possibly leading zeros, gaps between ones, trailing zeros.
Let the lengths of zero blocks be: a0 (before first one), a1 (between first and second), ..., a_m (after last one). Some of these may be zero if ones are adjacent or at edges.
We have sum_{i=0}^{m} a_i = n - m. And each a_i >= 0.
Now, number of all-zero substrings is sum over each zero block of length L: number of substrings fully within that block = L*(L+1)/2.
Since any all-zero substring cannot cross a one. So total zero substrings = sum_{i=0}^{m} a_i*(a_i+1)/2.
We want to minimize that sum given sum a_i = Z = n - m, and m+1 blocks.
By convexity (since function x(x+1)/2 is convex for x>=0), the sum is minimized when the a_i are as equal as possible (i.e., spread zeros as evenly as possible among the m+1 blocks). Because convex => for fixed sum, sum of convex functions is minimized when distribution is as equal as possible (or actually for convex, the sum is minimized by making distribution as "equal as possible"? Let's verify: For convex function, by Jensen, the sum is at least (m+1)*f(mean). But that's a lower bound. Actually for convex, given sum fixed, the sum of f(x_i) is minimized when the x_i are as equal as possible? Wait: Consider convex function like x^2. For fixed sum, sum of squares is minimized when numbers are as equal as possible (by QM-AM, yes). Actually convex function yields that the sum is minimized at extreme points? For convex, by Karamata, the vector with more equal components majorizes the vector with less equal? Actually majorization: A vector x majorizes y if it's more spread out. For convex functions, sum f(x_i) is larger when x is more spread out (if f is convex). So to minimize sum, we want the distribution as equal as possible (least spread). So indeed minimize sum of convex functions by making numbers as equal as possible.
Thus, the optimal is to distribute Z zeros among (m+1) blocks as evenly as possible: each block gets either floor(Z/(m+1)) or ceil(Z/(m+1)). But we also have constraints that a_i >= 0.
But careful: If m=0, then we have only one block of zeros of length n (since no ones). Then all-zero substrings = n*(n+1)/2, so f=0. That matches.
If m>0, we have m ones, so there are m+1 zero blocks.
We need to compute minimal sum of zero substring counts.
Let q = Z // (m+1), r = Z % (m+1). Then r blocks will have length q+1, and (m+1 - r) blocks will have length q.
But is this the minimal? Because zeros can be placed as gaps between ones. Since ones are identical, we can achieve any distribution of block lengths because we can space ones arbitrarily.
So minimal zero substrings = r * ((q+1)(q+2)/2) + (m+1 - r) * (q(q+1)/2).
Then maximum f = total substrings - that minimal zero substrings.
Total substrings = n*(n+1)//2.
Thus answer = total - minZero.
We can compute that.
But we need to handle m=0 separately? The formula works if m>0? For m=0, we have m+1=1 blocks, Z=n, q=n, r=0, then minZero = n*(n+1)/2, f=0. Works.
So general formula.
But we must verify with examples.
Example 1: n=3,m=1. Total = 34/2=6. Z=2. m+1=2. q=2//2=1, r=0? 2%2=0, r=0. So minZero = 2 * (12/2)=21=2? Actually 1(2)/2 =1. So sum=2. Then f=6-2=4. Correct.
Example 2: n=3,m=2. Total=6. Z=1. m+1=3. q=1//3=0, r=1%3=1. So blocks: r=1 block of length 1 (0+1?), q=0 so length 1; other 2 blocks length 0. minZero = 1 * (12/2)=1 + 2 * (01/2)=0 => total=1. f=5. Correct.
Now complexity: t up to 1e5, n up to 1e9, so we need O(1) per test case.
We need to compute safely with large numbers: up to n~1e9, n*(n+1)/2 ~ 5e17, fits in 64-bit (max about 5e17 < 9.22e18). So 64-bit unsigned/signed is fine (use long long in C++). In Python, unlimited.
Now we can implement.
But caution: When m=0, m+1=1, Z = n, q = n, r=0, minZero = n*(n+1)/2, f=0. Works.
But also consider m > n? Input says 0 <= m <= n, so fine.
Edge: When m > 0 but m+1 may be large; but Z can be less than m+1, leading q=0, r=Z. That's okay.
Thus answer = total - minZero.
Now confirm some other cases: n=4,m=2. Total=10. Z=2. m+1=3. q=0, r=2. minZero = 2 * (12/2) + (1)(0)=2 => f=8. Let's test manually: best placement? Possibly "1010"? Let's compute: "1010": substrings with at least one 1? Actually compute zeros: positions: 1:1,2:0,3:1,4:0. Zero blocks: a0=0 (before first 1), a1=0? Actually between first and second: after first one at position1, next one at position3, so gap length =1 (position2). Then trailing zeros after last one: position4 length1 => a2=1. So blocks lengths: 0,1,1 => zero substrings: 0+1+1=2? Wait compute each: length1 block contributes 1 substring (the single zero). Actually number of substrings in a block of length L is L*(L+1)/2: for L=1 =>1. So total zero substrings = 0+1+1=2. f=10-2=8. Can we do better? "1100"? Then zeros: trailing block of length2 => 23/2=3, plus maybe other blocks? Actually ones: positions 1,2 ones, zeros at 3,4 => a0=0? Before first one:0, between ones: they are adjacent => a1=0, trailing a2=2 => zero substrings=3, f=7. "1001"? zeros between: a0=0? Actually string "1001": positions:1=1,2=0,3=0,4=1 => blocks: a0=0, a1=2, a2=0 => zero substrings=23/2=3 => f=7. "0101": similar to "1010"? That's symmetric: zero blocks: leading0? "0101": pos1=0, pos2=1, pos3=0, pos4=1 => a0=1, a1=1, a2=0 => zero substrings=1+1=2 => f=8. So max is 8, matches formula. Good.
Thus solution is straightforward.
Now we need to output integer.
Implementation in Python: read t, loop, compute.
But note: there is also known alternative approach: maximize number of substrings containing at least one 1 = total pairs minus sum of zeros blocks combos. But we can also think: The optimal arrangement is to spread ones as far apart as possible? Actually to minimize zero substrings we want zeros to be in many small blocks rather than large ones. Since we have m ones, we have m+1 gaps. So we want zeros spread evenly among gaps. That is exactly what we did.
Thus answer = total - minZero.
Now we must be careful with integer arithmetic overflow in C++ but using long long (signed 64-bit) up to 5e17 < 9e18, so okay.
Now let's test with large n and m: n=1e9, m=5e8. Then total = 5e17, Z=5e8, m+1=5e8+1. q = Z // (m+1) likely 0 or 1? Actually Z=5e8, m+15e8+1, so q=0, r=Z =5e8. minZero = r*(1) =5e8. So f approx 5e17 -5e8 ~ 5e17. That's fine.
Now implement.
We must handle multiple test cases up to 1e5, so just compute quickly.
Now code in Python:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it)); m = int(next(it))
total = n*(n+1)//2
if m == 0:
out.append('0')
continue
Z = n - m
k = m + 1 # number of zero blocks
q = Z // k
r = Z % k
# minZero = r * ((q+1)(q+2)//2) + (k - r) * (q(q+1)//2)
# But careful: if q=0, then q*(q+1)//2 = 0, ok.
minZero = r * ((q+1)(q+2)//2) + (k - r) * (q(q+1)//2)
ans = total - minZero
out.append(str(ans))
sys.stdout.write('\n'.join(out))
But test with m=0: our code with if m==0. Actually we could let general formula work for m=0: k=1, Z=n, q=n, r=0, minZero = (1)(n(n+1)//2) = total, ans=0. So we can skip special case. However, when m=0, we have k=1, q=n, r=0, compute minZero = 0? Actually careful: (k - r) =1, q*(q+1)//2 = n*(n+1)//2, r part =0, so minZero = total, ans=0. Works. So we don't need special case.
But note: if m>0 but k might be large, but that's fine.
Thus final code.
Now let's double-check formula for minZero:
For a block of length L, number of all-zero substrings within that block is L*(L+1)//2.
If we have r blocks of length q+1, and (k - r) blocks of length q.
Then total = r * ( (q+1)(q+2)//2 ) + (k - r) * ( q(q+1)//2 ).
Yes.
Now we can test with given examples.
Edge: n=1,m=1. Total=1, Z=0, k=2? Wait m=1 => k=2. q=0//2=0, r=0. minZero = 0? Actually r=0, k-r=2, each q=0 => zero substrings=0. f=1. Is that correct? string "1": substrings containing at least one 1: only substring "1" => count=1. Good.
Edge: n=2,m=1: Total=3, Z=1, k=2, q=0, r=1. minZero = 1*(12//2)=1, plus (1)(0)=0 => total 1, f=2. Let's test: strings length2 with exactly one 1: "10": substrings: "1","0","10" => ones-containing: "1","10" =>2. "01": similarly 2. So max=2. Good.
Edge: n=2,m=2: Total=3, Z=0, k=3? Actually m=2 => k=3, q=0, r=0, minZero=0, f=3. "11": substrings: "1","1","11" all contain at least one 1? Actually "1" each contain 1, "11" contains ones, so 3. Yes.
So works.
Now consider large numbers: n=10^9, m=0 => ans=0, quick.
Now note: In some solutions, there is alternate formula: answer = total - ( (k - r) * C(q+1,2) + r * C(q+2,2) ), but essentially same.
Thus we have solution.
Now confirm time: t up to 1e5, per test O(1), fine.
Now we can produce final answer.
But wait, there is a nuance: The problem statement says n up to 1e9, m up to n. The formula uses division and multiplication with numbers up to about 1e9, but (q+1)*(q+2) could be up to (Z/(m+1) + 2)^2. Since Z up to 1e9, q up to 1e9. So product up to 1e18, still fits in Python int, but in C++ we need unsigned 64-bit maybe. In C++ long long (signed) up to 9e18, so it's safe.
Now we should consider if there is any alternate distribution that yields even lower zero substring count? For convex function, equal distribution minimizes sum. But we must verify that zero blocks can be distributed arbitrarily, including leading and trailing gaps. Yes, because we can place ones at positions that create gaps of desired lengths. For example, to have a0 = x0, a1 = x1, ..., a_m = x_m, we set first one after x0 zeros, then next after x1 zeros, etc. So any nonnegative integers summing to Z are achievable.
Thus equal distribution is indeed optimal.
Thus answer is correct.
Now we can finalize.
We should also consider if m = 0 case is separate to avoid division by zero? But m+1=1, no division by zero.
Now we are ready.
Now I'll produce final solution explanation and code.
We'll also mention that for m=0 answer=0.
Now implement in C++.
C++ code:
#include
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll total = n*(n+1)/2;
ll Z = n - m;
ll k = m + 1;
ll q = Z / k;
ll r = Z % k;
ll minZero = r * ((q+1)(q+2)/2) + (k - r) * (q(q+1)/2);
ll ans = total - minZero;
cout << ans << '\n';
}
return 0;
}
But ensure that multiplication doesn't overflow. Since (q+1)(q+2) could be up to ~(1e9+1)(1e9+2) ~ 1e18+... which fits in 64-bit signed if we use 64-bit. Actually maximum q could be Z (if m=0, then k=1, q=Z = n up to 1e9, so (q+1)*(q+2) approx 1e18+something < 2^63-1 (~9.22e18). So safe.
But careful: if m=0 then k=1, q=n, r=0 => (q+1)(q+2)/2 huge but still within signed 64-bit? For n=1e9, (1e9+1)(1e9+2) = 1e18 + 3e9 + 2 ≈ 1.000003002e18 < 9.22e18. So safe.
Thus fine.
Now we produce final answer.
But I think there's a known simpler formula: When zeros are distributed as evenly as possible, the number of all-zero substrings is (k - r)C(q,2) + rC(q+1,2)? Actually careful: C(L,2) + L = L*(L-1)/2 + L = L*(L+1)/2, so it's easier to compute using combination.
But anyway.
Now produce final answer.
Now I'd like to double-check with a random example: n=6,m=3. total=21. Z=3, k=4. q=3//4=0, r=3. minZero = 3*(1*2/2)=3 + (1)*0=3 => f=18. Let's manually check: best arrangement? 3 ones, 3 zeros, 4 gaps. We need to distribute zeros: 3 blocks get 1 zero each, one block zero length. So zeros are separated as much as possible, e.g., "101010"? Actually length 6, 3 ones => pattern 1 0 1 0 1 0 => zeros at positions 2,4,6. That yields zero blocks: leading? a0=0? Actually if string starts with 1: "101010": blocks: a0=0, a1=1 (between first and second), a2=1 (between second and third), a3=1 (trailing). So three blocks of length1, one block of length0. Zero substrings=1+1+1=3, f=21-3=18. Can we get lower? If we put zeros together, e.g., "111000": then trailing block length 3 => zero substrings=6, f=15. So indeed 18 is max. Good.
Thus solution stands.
Now answer with code.
But the problem has t up to 1e5, so we need fast I/O. In C++ use scanf/printf or with ios_base sync. In Python use sys.stdin.
Thus final.
We'll produce final answer with explanation.
We need to maximize the number of substrings that contain at least one '1'. Let total number of substrings be T = n(n+1)/2. If we denote the lengths of the zero blocks (gaps between ones) as a0, a1, …, am (there are m+1 blocks, possibly empty), then the number of all‑zero substrings is sum_{i=0}^{m} ai(ai+1)/2. Since the sum of all ai equals Z = n−m, minimizing this sum for a convex function gives the smallest sum when the ai are as equal as possible.
Let k = m+1, q = Z // k, r = Z % k. Then r blocks have length q+1 and k−r blocks have length q. The minimal number of all‑zero substrings is
minZero = r · (q+1)(q+2)/2 + (k−r) · q(q+1)/2.
The answer is T − minZero.
All calculations fit in 64‑bit integers. The solution works for all cases, including m = 0.
Complexity: O(1) per test case, O(t) total.
cpp
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll total = n * (n + 1) / 2;
ll Z = n - m;
ll k = m + 1;
ll q = Z / k;
ll r = Z % k;
ll minZero = r * ((q + 1) * (q + 2) / 2) + (k - r) * (q * (q + 1) / 2);
ll ans = total - minZero;
cout << ans << '\n';
}
return 0;
}