1790B: Taisia and Dice

We're given the n, s, and r, where

  • n is a number of 6-sided dice
  • s is the sum of a roll of said dice
  • r is the sum of the remaining dice when one of the maximum-value dice is "stolen"

The task is to reconstruct a possible roll (a set of n dice) that fits the information above. The detail that the stolen die is also the max value that appears in the roll is key. We can recover the value of the stolen die by taking s - r. From this point, reconstructing a valid roll is just a matter of evenly spreading the remaining sum, r, across n - 1 dice. Namely, set each remaining die to the integer average of the remaining sum. Then, assuming there is some leftover value to be added to the total due to integer truncation, spread this "surplus" evenly across the dice while making sure no individual value exceeds the stolen die's value.


_35
#include <iostream>
_35
#include <vector>
_35
_35
void solve() {
_35
int n,s,r;
_35
std::cin>>n>>s>>r;
_35
int stolen_value = s-r;
_35
std::vector<int> sequence (n-1);
_35
int needed = n-1;
_35
int avg = r/needed;
_35
std::fill(sequence.begin(), sequence.end(), avg);
_35
_35
int surplus = s - (stolen_value + needed * avg);
_35
_35
for (int i = 0; i < n-1 && surplus != 0; ++i) {
_35
int distance_from_max = stolen_value - sequence[i];
_35
int adding = std::min(surplus, distance_from_max);
_35
sequence[i] += adding;
_35
surplus -= adding;
_35
}
_35
_35
for (int x : sequence) {
_35
std::cout << x << ' ';
_35
}
_35
std::cout << stolen_value << '\n';
_35
}
_35
_35
int main() {
_35
int t;
_35
std::cin >> t;
_35
for (int tc = 1; tc <= t; ++tc) {
_35
solve();
_35
}
_35
return 0;
_35
}

If you found this solution helpful, consider leaving a star!