ORIGIN

AtCoder-4873 ModSum

ACM 1 mins244 words

ModSum

Problem Statement

For an integer N, we will choose a permutation ${P_1,P_2,\cdots,P_N}$ of ${1,2,\cdots,N}$.

Then, for each $i =1,2,\cdots,N$, let $M_i$ be the remainder when $i$ is divided by $P_i$.

Find the maximum possible value of $M_1+M_2+\cdots+M_N$.

Constraints

  • N is an integer satisfying 1≤N≤109.

Input

Input is given from Standard Input in the following format:

1
N

Output

Print the maximum possible value of $M_1+M_2+\cdots+M_{N}$.

Sample Input 1

1
2

Sample Output 1

1
1

When the permutation ${P_1,P_2}={2,1}$ is chosen, $M_1+M_2=1+0=1$.

Sample Input 2

1
13

Sample Output 2

1
78

Sample Input 3

1
1

Sample Output 3

1
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$ is mode by $1$ which is always equal to $1$ .

And you can easily find that the answer is
$$
1+2+3+\cdots+(n-1)+1 \= \frac{(1+(n-1))(n-1)}{2} + 1 \= \frac{n(n-1)}{2} + 1
$$

Code

1
2
3
4
5
6
7
8
9
10
#include<bits/stdc++.h>

using namespace std;

int main(){
long long n;
cin >> n;
cout << n * (n - 1) / 2 << endl;
return 0;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|