D. Plus Minus Permutation | Codeforces Solution | Codeforces Round 895 (Div. 3)


You are given 3 integers β€” 𝑛 , π‘₯ , 𝑦 . Let's call the score of a permutation† 𝑝1,…,𝑝𝑛 the following value: (𝑝1β‹…π‘₯+𝑝2β‹…π‘₯+…+π‘βŒŠπ‘›π‘₯βŒ‹β‹…π‘₯)βˆ’(𝑝1⋅𝑦+𝑝2⋅𝑦+…+π‘βŒŠπ‘›π‘¦βŒ‹β‹…π‘¦) In other words, the score of a permutation is the sum of 𝑝𝑖 for all indices 𝑖 divisible by π‘₯ , minus the sum of 𝑝𝑖 for all indices 𝑖 divisible by 𝑦 . You need to find the maximum possible score among all permutations of length 𝑛 . For example, if 𝑛=7 , π‘₯=2 , 𝑦=3 , the maximum score is achieved by the permutation [2,6⎯⎯,1⎯⎯,7⎯⎯,5,4⎯⎯⎯⎯,3] and is equal to (6+7+4)βˆ’(1+4)=17βˆ’5=12 .
D. Plus Minus Permutation
time limit per test: 1 second
memory limit per test: 256 megabytes

You are given 3 integers β€” n, x, y. Let's call the score of a permutation† p1, …, pn the following value:

(p1β‹…x + p2β‹…x + … + p⌊n/xβŒ‹β‹…x) βˆ’ (p1β‹…y + p2β‹…y + … + p⌊n/yβŒ‹β‹…y)

In other words, the score of a permutation is the sum of pi for all indices i divisible by x, minus the sum of pi for all indices i divisible by y.

You need to find the maximum possible score among all permutations of length n.

For example, if n = 7, x = 2, y = 3, the maximum score is achieved by the permutation [2, 6, 1, 7, 5, 4, 3] and is equal to (6 + 7 + 4) βˆ’ (1 + 4) = 17 βˆ’ 5 = 12.

† A permutation of length n is an array consisting of n distinct integers from 1 to n in any order. For example, [2, 3, 1, 5, 4] is a permutation, but [1, 2, 2] is not a permutation (the number 2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3, but the array contains 4).

Input

The first line of input contains an integer t (1 ≀ t ≀ 104) β€” the number of test cases.

Then follows the description of each test case.

The only line of each test case description contains 3 integers n, x, y (1 ≀ n ≀ 109, 1 ≀ x, y ≀ n).

Output

For each test case, output a single integer β€” the maximum score among all permutations of length n.

Example
Input
8
7 2 3
12 6 3
9 1 9
2 2 2
100 20 50
24 4 6
1000000000 5575 25450
4 4 1
Output
12
-3
44
0
393
87
179179179436104
-6
Note

The first test case is explained in the problem statement above.

In the second test case, one of the optimal permutations will be [12, 11, 2, 4, 8, 9, 10, 6, 1, 5, 3, 7]. The score of this permutation is (9 + 7) βˆ’ (2 + 9 + 1 + 7) = βˆ’3. It can be shown that a score greater than βˆ’3 can not be achieved. Note that the answer to the problem can be negative.

In the third test case, the score of the permutation will be (p1 + p2 + … + p9) βˆ’ p9. One of the optimal permutations for this case is [9, 8, 7, 6, 5, 4, 3, 2, 1], and its score is 44. It can be shown that a score greater than 44 can not be achieved.

In the fourth test case, x = y, so the score of any permutation will be 0.

Solution

Explanation:

The solution maximizes the score by calculating the number of positions divisible by x (to add large numbers) and y (to subtract small numbers), accounting for overlaps using their LCM (via GCD). It uses a helper function to compute the sum of integers in ranges, assigning the largest n/x numbers to x-divisible positions, excluding overlaps, and the smallest remaining n/y - n/LCM numbers to y-divisible positions, then outputs the difference.

int helper(int st, int end)
{
  int e = (end * (end + 1)) / 2;
  --st;
  int s = max(0LL, (st * (st + 1)) / 2);
  return e - s;
}

void solve()
{

  int n, x, y;
  cin >> n >> x >> y;

  int gcd = __gcd(x, y);
  gcd = (x * y) / gcd;

  int common = n / gcd;

  int cpy = common;

  int tot = n / x;
  int mn = n / y;

  int res = 0;
  int st = n;

  st -= tot;
  res += helper(n - tot + 1, n);
  res -= helper(st + 1, st + common);

  mn -= cpy;
  st = 1;
  mn = abs(mn);

  res -= helper(1, mn);

  cout << res << endl;
}
TagsD. Plus Minus Permutationcodeforces solutionCodeforces Round 895 (Div. 3)
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro Poster
CodingKaro
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro
Other Blogs in Codeforces