-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEasySum.java
More file actions
24 lines (20 loc) · 730 Bytes
/
Copy pathEasySum.java
File metadata and controls
24 lines (20 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
public class EasySum {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int testCases = SCANNER.nextInt();
while (testCases-- > 0) {
final int n = SCANNER.nextInt();
final int m = SCANNER.nextInt();
final int rows = ceil((double) (n - m + 1) / m) + 1;
final int k = m - ((n - m + 1) % m);
System.out.println(summation(m - 1) * rows - (summation(m - 1) - summation(m - k - 1)));
}
}
private static int ceil(double x) {
return (int) Math.ceil(x);
}
private static long summation(long x) {
return (x * (x + 1)) / 2;
}
}