See the original article https://dyingdown.github.io/2019/11/17/AtCoder-4873-ModSum/
ModSum
Problem Statement
For an integer N, we will choose a permutation { P 1 , P 2 , ⋯ , P N } \{P_1,P_2,\cdots,P_N\} {P1,P2,⋯,PN} of { 1 , 2 , ⋯ , N } \{1,2,\cdots,N\} {1,2,⋯,N}.
Then, for each i = 1 , 2 , ⋯ , N i =1,2,\cdots,N i=1,2,⋯,N, let M i M_i Mi be the remainder when i i i is divided by P i P_i Pi.
Find the maximum possible value of M 1 + M 2 + ⋯ + M N M_1+M_2+\cdots+M_N M1+M2+⋯+MN.
Constraints
- N is an integer satisfying 1≤N≤109.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible value of M 1 + M 2 + ⋯ + M N M_1+M_2+\cdots+M_{N} M1+M2+⋯+MN.
Sample Input 1
2
Sample Output 1
1
When the permutation { P 1 , P 2 } = { 2 , 1 } \{P_1,P_2\}=\{2,1\} {P1,P2}={2,1} is chosen, M 1 + M 2 = 1 + 0 = 1 M_1+M_2=1+0=1 M1+M2=1+0=1.
Sample Input 2
13
Sample Output 2
78
Sample Input 3
1
Sample Output 3
0
Analysis
The biggest answer is that after the operation the number can remain it self. To make this satisfy the most, you just need to mode this number by this number plus 1. $ 1 \space mod \space 2, 2,\space mod \space 3, \cdots, n-1 \space mod \space n$ . So The left number n n n is mode by 1 1 1 which is always equal to 1 1 1 .
And you can easily find that the answer is
1
+
2
+
3
+
⋯
+
(
n
−
1
)
+
1
=
(
1
+
(
n
−
1
)
)
∗
(
n
−
1
)
2
+
1
=
n
∗
(
n
−
1
)
2
+
1
1+2+3+\cdots+(n-1)+1 \\= \frac{(1+(n-1))*(n-1)}{2} + 1 \\= \frac{n*(n-1)}{2} + 1
1+2+3+⋯+(n−1)+1=2(1+(n−1))∗(n−1)+1=2n∗(n−1)+1
Code
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin >> n;
cout << n * (n - 1) / 2 << endl;
return 0;
}