Milly is playing with an Array A of size N. She wants to convert this array into a magical array. This magical array satisfies the condition Ai-1 < Ai where i ∈ [2, N] . She can add a value X to any element of this array any number of times. Your task is to tell her the minimum number of such addition of X are required in order to get the magical array.
Input
First line of the input will contain T(No. of test cases).
For every test case, first line will contain two space separated integers denoting N and X. Next line will contain N space separated integers denoting Ai.
Output
For every test case, print the required answer in a new line.
Constraints
1 ≤ T ≤ 10
2 ≤ N ≤ 10^5
1 ≤ X ≤ 10^9
1 ≤ Ai ≤ 10^9
SAMPLE INPUT
2
2 1
1 1
3 1
1 1 2
SAMPLE OUTPUT
1
2
Explanation
Test case #1: Add 1 to the second value.
Test case #2: Add 1 to the second value and add 1 to the third value.
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 are given an array A of size N. We want to convert it into a "magical array" which satisfies A_{i-1} < A_i for i from 2 to N (strictly increasing). We can add a value X (same X for all operations) to any element of the array any number of times. Each addition of X to an element counts as one operation? The problem says: "She can add a value X to any element of this array any number of times. Your task is to tell her the minimum number of such addition of X are required in order to get the magical array." So each time we add X to some element, it counts as one addition. So essentially we can increase any element by multiples of X, and the total number of operations is sum over elements of k_i where we add k_i * X to A_i. We want to minimize total number of operations (i.e., sum of k_i) subject to the resulting array B_i = A_i + k_i * X satisfying B_1 < B_2 < ... < B_N (strictly increasing). We can choose k_i >= 0 integers.
We need to find minimal total increments (each of X) to make array strictly increasing.
This looks like a greedy problem. We want to minimally increase elements to enforce increasing property, but we can only increase by multiples of X. Typically, if we could increase arbitrarily, we'd set B_1 = max(A_1, something) and then ensure B_i > B_{i-1} by raising as needed. Since we can only add multiples of X, the increments are quantized.
Let’s denote B_i = A_i + c_i * X, where c_i >= 0 integer. We want B_1 < B_2 < ... < B_N. Minimize sum c_i.
We can think of it as: We need to assign B_i such that B_i >= A_i, B_i ≡ A_i (mod X) (since adding multiples of X doesn't change modulo X) and strictly increasing. Also B_i can be any number >= A_i that is congruent to A_i mod X. So we want to pick the smallest possible numbers >= A_i that satisfies B_i > B_{i-1}.
We can process left to right. For i=1, we would ideally keep B_1 = A_1 (c_1 = 0) unless forced later? But later constraints only require B_i > B_{i-1}. Since we can only increase, we might need to increase earlier elements to allow later ones to be feasible? Actually, if we only increase later elements, we can always satisfy increasing by bumping up B_i to be > B_{i-1}. However, because we can only increase by multiples of X, there might be a situation where A_i mod X makes it impossible to exceed B_{i-1} without increasing B_i too much? But we can always increase B_i arbitrarily high by adding enough X, so it's always possible. So we never need to increase an earlier element to allow a later one to be possible. However, increasing earlier elements could reduce total operations? Consider: Suppose A_1=10, A_2=9, X=2. We need B_1 < B_2. If we set B_1=10 (c_1=0), then B_2 must be >10, smallest value >=9 and ≡9 mod2 is 9 (if 9>10? No) then 9 is less than 10, so we need to increase. The next possible is 11 (since 9+2=11) which is >10, so c_2=1 => one operation. Total 1. If we increased B_1 to 12 (c_1=1), then B_2 could be 9? 9<12, no. The smallest >12 is 13? 9+2=11<12, +2*2=13>12 => c_2=2 => total 3. So worse. So increasing earlier elements only adds cost; it doesn't reduce later cost because later elements must be > earlier, and we can always increase later. But is there a scenario where increasing earlier reduces later? Possibly if the modulo constraints force later to be much higher, maybe increasing earlier could align modulos to reduce later's needed increase? Actually, consider A_1=5, A_2=6, X=10. Need B_1<B_2. If we keep B_1=5, then B_2 must be >5. Smallest >=6 and ≡6 mod10 is 6, which is >5, so c_2=0 => total 0. That's fine.
But consider A_1=5, A_2=5, X=2. B_1=5, then B_2 must be >5, smallest >=5 and ≡5 mod2 (i.e., odd) is 5, but 5 is not >5, so need to increase to 7 (add 2) => c_2=1 total 1. If we increased B_1 to 7 (c_1=1), then B_2 could be 5? 5<7, so need B_2>7, smallest ≡5 mod2 and >7 is 9? Actually 5,7,9... 7 is not >7, so 9 => c_2=2 total 3. Worse.
What about case where later elements are already much larger but modulo misalignment could cause many increments if earlier is low? Actually, earlier being low is beneficial. So it seems optimal to keep earlier as low as possible (i.e., not increase unless necessary). However, is there any scenario where we need to increase an earlier element because later elements are bounded above? No, there is no upper bound, we can increase arbitrarily. So we can always achieve increasing by only increasing later elements as needed. So the minimal total operations is obtained by processing left to right, setting B_1 = A_1 (c_1=0). For i=2..N, set B_i to the smallest number >= A_i such that B_i > B_{i-1} and B_i ≡ A_i (mod X). This is essentially: we need to find the smallest multiple addition of X to A_i that makes it > B_{i-1}.
Compute: Let d = B_{i-1} + 1 - A_i. We need A_i + c_i * X >= B_{i-1} + 1. So c_i = ceil(max(0, d) / X). Actually d may be negative if A_i already > B_{i-1}, then we may not need any increase; but we need strictly greater: B_i > B_{i-1}. If A_i > B_{i-1}, we can set B_i = A_i (c_i=0) because A_i > B_{i-1}? If A_i > B_{i-1}, then A_i > B_{i-1} so condition satisfied. However, need to check if A_i might equal B_{i-1}? If A_i == B_{i-1}, we need >, so need to increase at least to B_{i-1}+1, but might need to align modulo. If A_i < B_{i-1}, definitely need increase.
But also there is modulo constraint: B_i must be congruent to A_i mod X. So we can't simply set B_i = max(A_i, B_{i-1}+1) because it might not have same remainder. We need the smallest number >= max(A_i, B_{i-1}+1) that is ≡ A_i (mod X). That is:
target = max(A_i, B_{i-1}+1)
We need to find smallest y >= target such that y ≡ A_i (mod X). Then c_i = (y - A_i) / X.
Since X is positive, we can compute:
Let r = A_i % X. Actually we need congruence: y mod X = A_i mod X. So given target, we can compute the remainder of target mod X, and adjust.
Let rem = A_i mod X. (But careful: mod with positive X). Let needed = (rem - (target % X) + X) % X. Then y = target + needed, unless needed == 0 then y = target. But we must ensure y >= target. So that works.
But note: If target % X == rem, then y = target. Else y = target + ((rem - target%X + X) % X). However, this may be less than target? Actually it adds a nonnegative offset less than X, so y >= target.
But also we must ensure y > B_{i-1}. Since target = max(A_i, B_{i-1}+1), it's already >= B_{i-1}+1 (if we pick target = B_{i-1}+1 when B_{i-1}+1 >= A_i, else A_i > B_{i-1}+1? Wait: If A_i > B_{i-1}, then target = A_i, which is > B_{i-1} because A_i > B_{i-1}. So condition satisfied. So y will be >= target, and if target = A_i > B_{i-1}, y>= A_i > B_{i-1}. So ok. If target = B_{i-1}+1, then y >= B_{i-1}+1, so > B_{i-1}. So condition satisfied.
Thus greedy left-to-right with no backtracking seems optimal.
But is it always optimal to not increase earlier elements? Let's test some tricky scenarios: suppose X=3, A = [1, 4, 4]. Process: B1=1. i=2: target = max(4, 2) = 4, 4%3=1, A2%3=1? 4%3=1, so target works, B2=4 (c2=0). Now i=3: B2=4. target = max(4, 5)=5. A3=4, rem=1. target%3=2 (5%3=2). needed = (1-2+3)%3=2, so y=5+2=7. B3=7, c3=(7-4)/3=1. Total ops=1. Could we do better? If we had increased B2 to something else? Suppose we increased B2 to 7 (c2=1) then B3? We want B3>7, A3=4, target=8. 8%3=2, need rem1, needed = (1-2+3)%3=2 => y=10, c3=2 => total 3. Worse. If we increased B1 to 2? But then B2 and B3 maybe less? B1=2 (c1=1, adding 1? Actually X=3 so add 3? No, we can only add multiples of X=3. So B1 can be 1,4,7,... If B1=4 (c1=1), then B2: target=max(4,5)=5, rem=1, target%3=2 => y=7, B2=7 c2=1; then B3: target=max(4,8)=8, need rem1 => y=10, c3=2; total 4. So indeed greedy minimal.
What about scenario where due to modulo, increasing a later element by X might cause a cascade requiring further increases in later elements. But since we always take minimal needed for each step given the previous B, that should be optimal because future decisions don't affect previous ones and we can always adjust later. It's a typical "make array strictly increasing with minimal increments" problem, and when increments are arbitrary real numbers, the greedy is keep as low as possible. With step size X, the greedy still works because we only increase the current element to the smallest feasible value, which doesn't hurt future.
Proof: Suppose we have an optimal solution B* with minimal total sum of (B_i - A_i)/X. If at i=1 we have B1* > A1, we can decrease B1* to A1 and adjust later elements? But decreasing B1 might break increasing condition for i=2 if B2 is close. However, we could then possibly decrease B2 as well? But careful: B2 must be >= A2 and congruent. If B1* > A1, then we can try to reduce B1* to A1 (which is lower) while keeping B2 the same? That would still satisfy B1 < B2 because B1 becomes smaller, so condition still holds (if originally B1* < B2). And we reduce operations by (B1*-A1)/X, which is positive, contradicting optimality unless B1* = A1. But could there be a scenario where B1* > A1 is necessary because otherwise B2 cannot be made congruent without exceeding something? However B2 can be increased arbitrarily, so we can always adjust B2 upward if needed, but lowering B1 never forces B2 to increase; it only relaxes the condition. So B1* should equal A1 in any optimal solution. By induction, after fixing B1=A1, for i=2, we want the minimal B2 that satisfies constraints. If optimal solution has B2 > minimal feasible, we can reduce it to minimal feasible (decrease operations) and still maintain B1 < B2 (since minimal feasible > B1). But would that affect later elements? It might allow later elements to be lower because the constraint from B2 is relaxed (B2 becomes lower). So we could possibly also lower later elements. So the total operations would be less or equal. So there exists an optimal solution where each B_i is minimal feasible given B_{i-1}. Thus greedy yields optimal.
So algorithm: For each test case, read N, X, array A. Initialize total_ops = 0. Set current = A[0] (first element with no addition). For i from 1 to N-1:
target = max(A[i], current + 1)
// compute smallest number >= target congruent to A[i] mod X
// we can do:
// let rem = A[i] % X
// needed = (rem - (target % X) + X) % X // this gives the offset to add to target to match remainder.
// y = target + needed
// However, careful: if needed == 0, y = target.
// Then ops_i = (y - A[i]) / X
// total_ops += ops_i
// current = y
But we need to consider that A[i] might be huge, and target might overflow? Use 64-bit (long long). X up to 1e9, Ai up to 1e9, N up to 1e5, but current may become larger than 1e9? Potentially after many increments, current could become large. But within 64-bit it's fine (max operations might be many, but worst-case if each step requires adding many X, current could become up to maybe initial max + N*X? Actually in worst case if array is decreasing heavily, we may need to raise each element significantly. For N=1e5, X=1e9, A_i=1, then to make strictly increasing we would set B1=1, B2=2 (if X=1, but X=1e9? Wait X can be large, but we add multiples of X, so increments are in steps of X. If X=1e9, then to get strictly increasing we may need to add large multiples. For worst case, if array is all 1's, B1=1, B2 must be >1 and ≡1 mod 1e9, so smallest is 1e9+1? Actually 1 % 1e9 = 1. So numbers congruent to 1 mod 1e9 are 1, 1e9+1, 2e9+1,... So target = max(1, 2)=2. The smallest number >=2 that is ≡1 mod 1e9 is 1e9+1. So B2=1e9+1, then B3 must be >1e9+1, target = max(1, 1e9+2) = 1e9+2, smallest ≡1 mod 1e9 is 2e9+1. So B3=2e9+1, etc. So after N elements, B_N ≈ (N-1)*1e9 + 1. For N=1e5, that's about 1e14, which fits in 64-bit (2^63-1 ~9e18). So use long long.
Edge Cases: X could be 1. Then any integer is congruent to A_i mod 1? Actually mod 1 everything is 0, but A_i mod 1 is 0 always. So any integer works. Then our computation: rem = A[i] % 1 = 0. target%1 = 0. needed = (0-0+1)%1 = 0? Actually modulo 1, (something) % 1 = 0 always. So needed = 0. y = target. ops = target - A[i] (since X=1). That is correct: we just need to make B_i = max(A_i, current+1). That's typical.
Also need to consider large T but sum N across tests might be up to 1e5? Actually constraints: T ≤ 10, N ≤ 10^5 each, so total N up to 1e6, okay.
Implementation details: Compute remainder: rem = A[i] % X. However, if X is large, A[i] % X is fine. But we also need target % X. Since target could be > 2^63, but mod X is okay using modulo operator with long long.
Better to compute needed directly:
We want the smallest k >= 0 such that A[i] + kX >= target. So k = ceil((target - A[i]) / X) if target > A[i] else 0. But also we need B_i = A[i] + kX. That's simpler: k = max(0, ceil((target - A[i]) / X)). However, because of strict >, we need B_i > current, but target already ensures that. So we can compute:
if A[i] > current:
// we can set B_i = A[i] if A[i] > current, because > holds? Actually need strictly greater: if A[i] > current, then yes. So k = 0.
else:
// A[i] <= current, need to exceed current. So we need smallest k such that A[i] + kX > current.
// So k = floor((current - A[i]) / X) + 1. Because we need A[i] + kX > current => kX > current - A[i] => k > (current - A[i]) / X => k = floor((current - A[i]) / X) + 1.
// Then B_i = A[i] + kX.
But wait: If A[i] > current, we set k=0 and B_i = A[i]. That works. However, is there a scenario where A[i] > current but not congruent? No issue, we take exactly A[i]. So the algorithm becomes:
If A[i] > current:
current = A[i]; // ops+=0
Else:
// A[i] <= current
let diff = current - A[i];
// we need to find smallest integer k such that A[i] + k*X > current
// That's equivalent to k > diff / X.
// So k = diff // X + 1.
// But careful: if diff is exactly multiple of X, then A[i] + (diff/X)*X = current, not >, so need one more.
// So k = floor(diff / X) + 1.
// Then new_val = A[i] + k * X.
// ops += k
// current = new_val
Is this equivalent to the earlier target method? Let's test: if A[i] <= current, target = current+1. Then we want smallest y >= current+1 with y ≡ A[i] mod X. Equivalent to smallest k such that A[i]+kX >= current+1. Since A[i]+kX is integer. So k = ceil((current+1 - A[i]) / X). That's ceil((current - A[i] + 1)/X). Because (current - A[i]) may be integer. Let d = current - A[i] (>=0). Then we need k such that A[i]+kX >= A[i]+d+1 => kX >= d+1 => k >= ceil((d+1)/X). Since d and X are integers, ceil((d+1)/X) = floor((d+1 - 1)/X) + 1? Actually ceil(a/b) = (a + b - 1)//b for positive ints. So k = (d + 1 + X - 1) // X = (d + X) // X. But also floor(d/X) + 1 =? Let's compare. Let d = qX + r, where 0 <= r < X. Then floor(d/X) = q. floor(d/X)+1 = q+1. Compute (d+X)//X = (qX + r + X)//X = q + 1 + (r // X) = q+1 because r<X. So (d+X)//X = q+1 if r>=0? Actually r can be 0..X-1, then d+X = q*X + r + X = (q+1)X + r. Dividing by X gives q+1 + r/X, but integer division truncates, so it's q+1 because r/X <1. So (d+X)//X = q+1 as well. So indeed k = floor(d/X)+1 = (d+X)//X. Also check: d = current - A[i]. So both formulas same. So the simpler method: if A[i] <= current: k = (current - A[i]) / X + 1 (integer division floor). But careful: (current - A[i]) may be negative? No, we are in else case so it's non-negative. So k = (diff // X) + 1. That yields integer. Then new_val = A[i] + kX.
But wait: what about the scenario where A[i] > current but A[i] - current is not a multiple of X? That's fine, we don't need to adjust.
But does this also cover the case when A[i] > current but A[i] == current? That can't happen because if A[i] > current, it's strictly >, so not equal. But if A[i] == current, then we are in else branch (since A[i] <= current). So we treat as needing increase.
Now, what about the scenario where A[i] > current but due to modulo, we might be forced to increase A[i] anyway? For example, suppose X=3, current=4, A[i]=5. 5 > 4, so we would set B_i=5 (k=0). That satisfies > and mod. No problem.
Thus algorithm:
total = 0
cur = A[0]
for i from 1 to N-1:
if A[i] > cur:
cur = A[i]
else:
diff = cur - A[i] # non-negative integer
k = diff // X + 1
total += k
cur = A[i] + k * X
This seems simpler than modulo calculation with target.
But wait, is there any nuance when X is 0? X >=1 per constraints.
However, consider a case: X=5, current=10, A[i]=6. diff=4, k = 4//5 +1 = 0+1=1, new_val=6+5=11 >10. Good.
But what about case: current=10, A[i]=10. diff=0, k = 0//5+1=1, new_val=10+5=15>10. Good.
Now consider a case: current=10, A[i]=9, X=2. diff=1, k=1//2+1=0+1=1, new_val=9+2=11>10. Good. If we used target method: target=11, rem=9%2=1, target%2=1 => y=11, ops=1. Good.
Edge case: current might become huge, but operations k could be large (up to maybe (current - A[i]) / X + 1). However, current grows, but total ops could be large. We need to use 64-bit for total_ops.
Now consider more tests:
Case: N=3, X=2, A=[1,3,5]. cur=1, i=1: A[1]=3 > cur, so cur=3. i=2: A[2]=5 >3, cur=5. total=0. Should be magical? 1<3<5, yes.
Case: N=3, X=2, A=[1,2,3]. cur=1, i=1: A[1]=2 >1, cur=2. i=2: A[2]=3 >2, cur=3. total=0. Works.
Case: N=3, X=2, A=[2,1,4]. cur=2, i=1: A[1]=1 <=2, diff=1, k=1//2+1=0+1=1, total=1, cur=1+2=3. i=2: A[2]=4 >3, cur=4. total=1. Check: after ops: [2,3,4] magical? 2<3<4, yes. Could there be better? If we increased first element? No.
Case: N=4, X=3, A=[5,5,5,5]. cur=5, i=1: diff=0, k=1, total=1, cur=5+3=8. i=2: diff=8-5=3, k=3//3+1=1+1=2? Wait: diff=3, 3//3=1, so k=2, new_val=5+6=11, total=3, cur=11. i=3: diff=11-5=6, k=6//3+1=2+1=3, new_val=5+9=14, total=6, cur=14. i=4: diff=9? Actually diff=14-5=9, k=9//3+1=3+1=4, new_val=5+12=17, total=10. So total ops=10. Check manually: we need strictly increasing. Starting 5, the next must be >5 and ≡5 mod3 (i.e., numbers: 5,8,11,14,...). So smallest sequence: 5,8,11,14,... That's 5,8,11,14 for N=4: ops: (8-5)/3=1, (11-5)/3=2, (14-5)/3=3 => total 6? Wait, (11-5)=6 => 6/3=2, yes. So we get 5,8,11,14. But above we got 5,8,11,14? Let's recalc: i=1: from A2=5, we set cur=8 (since diff=0, k=1 => 5+3=8). total=1. i=2: A3=5, cur=8, diff=3, k=1+1? Actually diff=3 => floor(3/3)=1, so k=2? That gives 5+6=11, correct. total becomes 1+2=3. i=3: A4=5, cur=11, diff=6 => floor(6/3)=2, k=3, new_val=5+9=14, total=6. So total=6, not 10. I miscomputed i=4; there is no i=4 because N=4, indices 0,1,2,3? Actually array indices 0..3, we processed i=1 (index1), i=2 (index2), i=3 (index3). That's three steps. So total=6. Good.
Now, is there any scenario where our simple formula fails? Let's test scenario where A[i] > current but we might want to increase it anyway to reduce future operations? The idea is: increasing current element may allow later elements to have smaller increases if it aligns modulos? But increasing current element makes it larger, which can only make later constraints harder (they need to be > larger number), so it wouldn't reduce later increases. However, there might be a scenario where due to modulo, increasing the current element by some X could change its residue, making it equal to the residue of later elements, thereby allowing later elements to be exactly current+ something small? But note: we cannot change the residue of the current element arbitrarily because we can only add multiples of X, which doesn't change residue mod X. So residue of B_i is fixed equal to A_i mod X. So we cannot change residue of any element; it's fixed. So aligning residues is not possible: each element has its own fixed residue determined by original A_i and X (since adding multiples doesn't change residue). So B_i mod X = A_i mod X always. So the residues are fixed. So the only way to reduce future increases is to have a smaller B_i, not larger. So we should keep B_i as low as possible.
Thus greedy is optimal.
Therefore solution: For each test case, read N, X, array A of long longs. Initialize cur = A[0], ops = 0. For i = 1 to N-1:
if A[i] > cur: cur = A[i];
else:
diff = cur - A[i]; // non-negative
// compute k = (diff) / X + 1? But integer division floors.
// k = diff // X + 1
// But careful: diff // X yields integer division truncating toward zero; since diff>=0, fine.
// Then ops += k
// cur = A[i] + k * X
But wait, consider case where diff // X may be large. But that's fine.
Now, could there be any overflow in computing cur? cur = A[i] + kX. Since A[i] up to 1e9, k could be up to maybe (cur - A[i])/X + 1, and cur itself could be huge up to ~1e14, but kX might be up to cur - A[i] + X, so it's roughly cur. So cur could increase further. Use 64-bit.
Edge Cases: X might be 1, diff // 1 = diff, k = diff+1, then cur = A[i] + (diff+1)*1 = cur+1? Because A[i] + (cur - A[i] + 1) = cur+1. So it becomes exactly cur+1. That makes sense: we just bump to one more than previous.
Now test some random cases with brute force for small N and X to verify.
But before finalizing, let's think if there is any subtle condition: The magical array condition is A_{i-1} < A_i for i ∈ [2, N]. So we only need strict increasing, not necessarily non-decreasing. Our method ensures that.
But what about the possibility of adding X to earlier elements to reduce total? Consider residues fixed. Suppose A = [2, 1] and X=3. Residues: 2%3=2, 1%3=1. We have cur=2, A2=1 <= cur, diff=1, k = 1//3+1=0+1=1, new_val=1+3=4, total=1, cur=4. Sequence: 2,4. Could we instead increase A1 to 5 (c1=1) and keep A2=1? Then we have 5 and 1, but 1<5 so need increase A2 to >5, smallest ≡1 mod3 and >5 is 7 (since 1,4,7,... 4<=5, 7>5) => c2=2 (since 7-1=6/3=2). total=3. Worse. So no.
What about scenario where residues align if we increase earlier element? But residues are fixed; increasing earlier doesn't change residue, so alignment is independent. Actually if we increase earlier, we might make the difference between residues such that later element's next feasible value is not far above? But the later element's feasible values are spaced by X. The distance from current to next feasible later value depends on current value and later residue. If we increase current, the next feasible later value might become current + something smaller? Let's analyze: we have current value cur, and later element with base value A_j and residue r_j. The smallest y >= cur+1 with y ≡ r_j (mod X) is y = cur+1 + ((r_j - (cur+1) mod X + X) % X). That offset is between 0 and X-1. So the increment needed for later is (y - A_j)/X. If we increase cur by Δ (multiple of X), then cur' = cur + Δ. Then y' = cur+Δ+1 + offset', where offset' = ((r_j - (cur+Δ+1) % X + X) % X). The increment for later becomes (y' - A_j)/X. Since Δ is multiple of X, (cur+Δ+1) mod X = (cur+1) mod X + (Δ mod X) = (cur+1) mod X, because Δ mod X = 0. So offset' = offset. Thus y' = cur+Δ+1+offset = y + Δ. So increment for later becomes (y+Δ - A_j)/X = (y - A_j)/X + Δ/X. So increasing cur by Δ adds Δ/X to the required operations for later. So it's never beneficial. So greedy stands.
Thus solution is O(N) per test case.
But wait, there is a nuance: In our else branch, we used k = diff // X + 1. But is there any scenario where A[i] is less than cur but diff is such that A[i] + (diff // X) * X could be equal to cur? For example, diff = 5, X=3. diff//X = 1, so A[i] + 3 = cur - 2? Actually if diff = 5, then cur = A[i] + 5. Adding 3 gives A[i]+3, which is cur-2, still less than cur. So we need one more X to exceed cur. So k = 2 gives A[i]+6 > cur (5). So correct.
But consider if diff is exactly multiple of X. diff = 6, X=3. diff//X=2. Then A[i] + 6 = cur, not > cur, so we need k = 3. diff//X+1 = 2+1=3. Good.
Thus correct.
Now confirm with more examples, including large values.
Potential pitfalls: Using int for diff, but diff can be up to cur - A[i] where cur might be up to ~1e14, so we need long long for diff.
Also careful about reading input and output.
We need to handle T test cases.
Now let's implement:
Pseudo:
int t;
cin >> t;
while(t--){
int n;
long long x;
cin >> n >> x;
vector a(n);
for(int i=0; i<n; ++i) cin >> a[i];
long long cur = a[0];
long long total = 0;
for(int i=1; i<n; ++i){
if(a[i] > cur){
cur = a[i];
}else{
long long diff = cur - a[i]; // non-negative
long long k = diff / x + 1; // integer division
total += k;
cur = a[i] + k * x;
}
}
cout << total << endl;
}
But wait: diff/x using integer division works because both positive. However, diff might be very large, but that's fine.
Now test sample.
Now test some random cases with brute force for small N and X to be sure.
We'll implement a brute force for small N (like N <= 6, X small, values small) to verify greedy equals minimal total ops.
Brute force: For each element, we can choose k_i up to some bound (like max needed). The total operations is sum k_i. Since values are small, we can search over k_i up to some limit, but we need to guarantee minimal. Instead, we can formulate as DP: For each i, we need to ensure B_i > B_{i-1}. Since we can only increase, we can set B_i as A_i + k_i * X. For each i, we can try possible k_i that yields B_i >= something, but the state space might be large. However for small N and small numbers we can brute force by enumerating k_i up to a limit (like 5) and check constraints. But to be thorough, we can use the fact that the optimal B_i is known to be the minimal feasible given previous; but we want to confirm if there is any case where increasing earlier reduces total. I'll try to generate random small arrays and compare greedy result with a BFS/DP that computes minimal total ops by trying all possible k_i up to a certain bound that covers the optimum. Since X may be > values, bound maybe small.
Given the reasoning above, I'm confident.
But let's do some mental checks.
Edge case: X is huge relative to values. Example: N=3, X=100, A=[1, 1000, 2]. Process: cur=1, i=1: a[1]=1000 > 1 -> cur=1000. i=2: a[2]=2 <= 1000, diff=998, k=998//100 + 1 = 9+1=10? Actually 998/100 = 9 (since integer division truncates), +1 =10, so new_val=2+10*100=1002, total=10. Sequence: 1,1000,1002, magical? 1<1000<1002, yes. Could there be a better? If we increased a[1] maybe? But a[1] is already 1000. If we increased a[0] to something, it would only increase ops. So seems minimal.
Now consider a tricky case: A=[5, 6, 5], X=5. Residues: 5%5=0, 6%5=1, 5%5=0. Greedy: cur=5, i=1: a1=6 >5, cur=6. i=2: a2=5 <=6, diff=1, k=1//5+1=0+1=1, new_val=5+5=10, total=1, cur=10. Sequence: 5,6,10. Total ops=1. Check if we can do with 0 ops? 5,6,5 not increasing. So 1 is minimal? Could we do something else: maybe increase a1 to 10 (k= (10-6)/5=0.8 not integer? Actually 6+? to get to 10 would be 4, not multiple of 5. So can't. If we increase a1 to 11 (k=1, 6+5=11), then a2=5 needs >11, smallest ≡0 mod5 >11 is 15, ops for a2 = (15-5)/5=2, total=3. Worse. If we increase a0 to 10 (k=1), then a1=6 needs >10, smallest ≡1 mod5 >10 is 11, ops a1= (11-6)/5=1, a2=5 needs >11, smallest ≡0 mod5 >11 is 15, ops a2=2, total=4. So greedy optimal.
Another: A=[2, 7, 3], X=3. residues: 2%3=2, 7%3=1, 3%3=0. Greedy: cur=2, i=1: 7>2 -> cur=7. i=2: 3<=7, diff=4, k=4//3+1=1+1=2, new_val=3+6=9, total=2. Sequence:2,7,9. Check if maybe increase a1 to 10? 7->10 (k=1) then a2=3 needs >10, smallest ≡0 mod3 >10 is 12 (since 3,6,9,12), ops a2= (12-3)/3=3, total=4. Or increase a0 to 5 (k=1) then a1=7>5? Actually a1=7>5, so cur=7, a2 same as before, total=1+2? Actually a0 increase adds 1, then a2 same 2 => total 3. Or increase a0 to 8 (k=2) then a1=7? But 7 <8, need increase a1 to >8, smallest ≡1 mod3 >8 is 10? Actually numbers: 1,4,7,10,13... so 10 >8, ops a1= (10-7)/3=1, a2=3 needs >10, smallest 0 mod3 >10 is 12, ops=3, total=2+1+3=6. So greedy is best.
Now consider case where A[i] > cur but maybe increasing it to the next multiple could help later? For instance, residues might cause later to need a large jump if cur has a certain residue, but if we increase cur (which doesn't change residue) by a multiple of X, residue stays same, so later's offset from cur+1 remains the same offset, so the required increase for later increases by exactly the increase in cur divided by X. So it's never beneficial.
Thus algorithm is correct.
But wait, there's a nuance: In the else branch, we compute k = diff // X + 1. But what if diff is negative? That can't happen because we are in else branch (A[i] <= cur). So diff >= 0.
Now, ensure we use long long for all variables.
One more thing: The operation count is the total number of times we add X, not the total amount added. So our k is exactly that.
Now, check time complexity: O(N) per test case.
Now, what about memory? Just store array or we can process on the fly? Since we need A[i] for each i, we can read all into vector or process as we read. But we need to compare with previous cur which depends on earlier A values, so we can read first element, then for each subsequent, read and process. That would be O(1) memory. However, input is given as all in one line; we could just read the whole array. But we can also process streaming. Might be simpler to read all.
Given constraints up to 1e5, reading all is fine.
Now, double-check the problem statement: "She can add a value X to any element of this array any number of times." So each addition counts as one operation. So our approach is correct.
Now, consider that we might be able to add X to an element multiple times; we count each addition separately? Usually, they say "number of such addition" meaning each time you add X. So if we add k times, that's k operations. So yes.
Now, does the order of addition matter? No.
Thus answer is sum of k_i.
Now, I'll implement solution in C++.
But wait, there is a possible nuance: The minimal number of additions could be huge, up to maybe 1e14? That's okay for 64-bit.
Edge: N=2, X=10^9, A=[1, 10^9+2]. cur=1, i=1: 10^9+2 >1, cur=10^9+2, total=0. But note 10^9+2 is >1, so magical. So output 0.
Edge: N=3, X=2, A=[1000000000, 1, 1000000000]. cur=1e9, i=1: A1=1 <= cur, diff=999999999, k = diff//2 +1 = 499999999 +1 = 500000000? Actually diff=999,999,999, diff//2 = 499,999,999 (since integer division), +1 = 500,000,000, total=500M, cur=1 + 500M2 = 1 + 1,000,000,000 = 1,000,000,001. i=2: A2=1e9 = 1000000000 <= cur? cur=1,000,000,001, so A2=1,000,000,000 is less, diff=1, k=1//2+1=0+1=1, total=500,000,001, cur=1e9+2=1,000,000,002. So total large but within 64-bit (max maybe about 5e13 if N=1e5 and each step adds large? We'll compute bound: worst case N=1e5, X=1, array all 1. Then diff at step i is (i) because cur becomes i+1? Actually for X=1, we get cur = i+1 after i steps? Let's compute: start cur=1. For each subsequent, diff = cur - 1, k = diff+1 = cur, new cur = 1 + (cur)1 = cur+1. So after each step, cur increases by 1. So total ops = sum_{i=1}^{N-1} (i+1?) Actually start cur=1. For i=1 (2nd element): diff=0, k=1, new cur=2, total=1. i=2: diff=1, k=2, new cur=3, total=3. i=3: diff=2, k=3, total=6. So total = (N-1)N/2? Actually sum of first (N-1) numbers? For N elements, we have N-1 steps. After step i (1-indexed among steps), cur becomes i+1, and k_i = i? Let's see: step1: k=1, step2: k=2, step3: k=3,... step (N-1): k=N-1. So total = (N-1)N/2. For N=1e5, that's ~5e9, which fits in 64-bit. For X>1, k_i could be larger because diff/X may be less, but diff may be larger. Actually worst total may be when X is huge? If X is huge, the increments are large steps, so k_i may be 1 each step but diff might be large? But diff is cur - A_i, and cur grows. Let's try worst-case scenario for large X: X = 1e9, all A_i = 1. Then as computed earlier, after first step, cur becomes 1e9+1, k1=1. After second step, diff = (1e9+1)-1 = 1e9, k2 = 1e9/1e9 + 1 = 1+1=2, cur = 1 + 21e9 = 2e9+1, total=3. After third step, diff = 2e9, k3 = 2+1=3, cur = 3e9+1, total=6. So k_i = i, same as before! Actually pattern: k_i = i, because each step we need to add i multiples of X to exceed previous cur. Indeed, cur after step i = iX + 1? Actually base: start cur0 = 1. After step1: cur1 = X+1. After step2: cur2 = 2X+1? Let's check: A=1, step2: diff = (X+1)-1 = X, k2 = X/X+1 = 1+1=2, new cur = 1+2X = 2X+1. After step3: diff = (2X+1)-1 = 2X, k3 = 2+1=3, cur = 1+3X = 3X+1. So indeed k_i = i, cur_i = iX + 1? Wait step i (i from 1 to N-1) yields cur_i = iX + 1? For i=1: 1*X+1 = X+1, correct. For i=2: 2X+1, correct. So total ops = sum_{i=1}^{N-1} i = (N-1)N/2, same as X=1 case. So total ops is independent of X? Not exactly; if A_i are different, maybe different. But upper bound on total ops is (N-1)N/2? That is about 5e9 for N=1e5, fits in 64-bit (since 2^63-1 ~9e18). So using long long safe.
But consider scenario where X=1 and A is decreasing heavily: e.g., A = [10^9, 10^9-1, 10^9-2, ...]. Then cur initially = 10^9, next A2=10^9-1 <= cur, diff=1, k=2? Actually X=1: diff=1, k=1//1+1=1+1=2? Wait diff = cur - A2 = 1, k = diff//1 +1 =1+1=2, new cur = A2 +2 = 10^9+1, total=2. Next A3=10^9-2, diff = (10^9+1) - (10^9-2) = 3, k=3+1=4? Actually k= diff//1+1 = 3+1=4, new cur = (10^9-2)+4 = 10^9+2, total=6. So total grows quadratically. For N=1e5, worst-case total could be about (N-1)N/2 = 5e9, still fits.
But what if X is very small, like 1, we already considered.
Thus 64-bit is fine.
Now, we must be careful about integer division with negative numbers, but all non-negative.
Thus final solution is straightforward.
But wait: The problem statement says "She can add a value X to any element of this array any number of times." Could it be interpreted that each addition of X counts as one addition regardless of which element? That is what we assumed. Yes.
Now confirm with sample: matches.
Now think about if there is any possibility to reduce total ops by adding X to some element multiple times at once, but that's just multiple additions anyway.
Now, let's consider potential alternative interpretations: Some might think that each addition can be applied to any element, and we count the total number of addition operations, where each operation selects an element and adds X. So if we decide to add 3X to an element, that's 3 operations (unless we can add multiple at once?). The phrase "any number of times" suggests each addition is a separate act. So yes.
Now, finalize solution.
But before finalizing, let's double-check with some randomly generated cases using a small brute force solver (simulate mentally or write quick script, but I'll reason).
For N=3, X=4, A=[3,2,1].
Residues: 3%4=3, 2%4=2, 1%4=1.
Greedy:
cur=3
i=1: A1=2 <=3, diff=1, k=1//4+1=0+1=1, new cur=2+4=6, total=1.
i=2: A2=1 <=6, diff=5, k=5//4+1=1+1=2, new cur=1+8=9, total=3.
Sequence:3,6,9. Ops=3.
Could there be a better? Try alternative: Increase A0? If we increase A0 to 7 (k=1), then cur=7, i=1: A1=2 <=7, diff=5, k=5//4+1=2? Actually 5//4=1, +1=2 => new cur=2+8=10, ops so far:1 (for A0) +2=3. i=2: A2=1 <=10, diff=9, k=9//4+1=2+1=3, new cur=1+12=13, total=6. Worse.
If we increase A0 to 11 (k=2), total would be larger.
If we increase A1 instead of A0? Actually we must respect order; we can increase any elements. Suppose we keep A0=3, A1=2 but increase A1 more than minimal? That would increase total.
What if we increase A0 to 3 (no), A1 to something else? Minimal seems 3.
Try possibility: Make array: 3, 6, 9 is one. Could we have 3, 2->? Wait we need strictly increasing. If we set A0=3, A1=2 (needs increase). Could we set A1=2+? Actually 2+4=6, that's minimal >3. Then A2=1, need >6, minimal is 1+8=9, as above. So total 3. Could we do 3, 6, 5? No because 5 not >6. Could we do 7, 2, 1? 7 > 2 not good. So minimal 3 seems plausible.
What if we increase A2 first? Not helpful.
So greedy works.
Now consider N=4, X=3, A=[1,4,7,10] already increasing. total=0.
Consider N=4, X=3, A=[1,4,7,9] residues: 1%3=1,4%3=1,7%3=1,9%3=0. Greedy:
cur=1
i=1: 4>1, cur=4
i=2: 7>4, cur=7
i=3: 9>7? 9>7 yes, so cur=9, total=0. But check: residues? 9%3=0, but it's allowed because we don't require any modulo condition on final array besides being increased by multiples of X; we just need to add multiples, so we can leave as is if it already satisfies >. So yes total=0.
But is it magical? 1<4<7<9, yes. So ok.
Now consider a case where A[i] > cur but A[i] mod X may not equal its original? Actually it's fine.
Now, we should also consider if we can add X to elements any number of times, but we cannot subtract. So only increasing.
Thus solution is correct.
Now, implement in C++.
But before final coding, think about potential integer overflow when computing k * X: k up to maybe 5e9, X up to 1e9, product up to 5e18, fits in 64-bit (signed up to 9e18). cur = A[i] + k*X could be up to maybe 1e9 + 5e18 = 5e18, still within 9e18. So safe.
Now, we should also consider if we need to handle case where X = 0? Not allowed.
Now, code.
But there is an alternative approach using the formula: For each i, compute needed value. But our current method is simpler.
But wait, what about the case where A[i] > cur but still we might want to increase it to a higher value because maybe A[i] > cur but A[i] mod X gives a residue that later would cause large jumps? As argued, increasing cur by adding to current element (which is A[i] > cur) will increase cur, making later more expensive, and doesn't change residue. So not beneficial.
Thus finalize.
Now, final answer: For each test case, output total ops.
Double-check sample.
Now, also ensure that we use long long for reading X, as X could be up to 1e9, fits in 32-bit, but in multiplication we need 64-bit, so we can cast.
I'll write:
int T; cin >> T;
while(T--) {
int N; long long X;
cin >> N >> X;
vector A(N);
for(int i=0; i<N; i++) cin >> A[i];
long long cur = A[0];
long long ans = 0;
for(int i=1; i<N; i++) {
if(A[i] > cur) {
cur = A[i];
} else {
long long diff = cur - A[i];
long long k = diff / X + 1;
ans += k;
cur = A[i] + k * X;
}
}
cout << ans << endl;
}
But be careful: If X is a long long, diff / X is integer division of two long longs, works.
Now, test with sample.
Now, consider potential pitfalls: The array elements may not fit in int, so use long long.
Now, is there any scenario where A[i] > cur but A[i] - cur is not a multiple of X? That doesn't matter.
Now, final answer.
But before concluding, let's consider if we could possibly add X to earlier elements to reduce total ops due to "wrap-around" in modulo? Because we are only adding multiples, residues fixed. So no.
Thus solution is final.
Now, produce final response with explanation and code.
Also, note that the problem might be from HackerEarth or similar, and they expect answer as described.
Now, write final answer. ```cpp
#include
#include
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int T;
cin >> T;
while (T--) {
int N;
ll X;
cin >> N >> X;
vector<ll> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
ll cur = A[0]; // value of the previous element after possible additions
ll ans = 0; // total number of additions
for (int i = 1; i < N; ++i) {
if (A[i] > cur) {
cur = A[i]; // no addition needed
} else {
ll diff = cur - A[i]; // how far A[i] is behind
ll k = diff / X + 1; // smallest integer with A[i] + k*X > cur
ans += k;
cur = A[i] + k * X; // new value for this position
}
}
cout << ans << "\n";
}
return 0;