1

Given $N$, $K$ and $S$, we have to find out a way to represent $S$ as a sum of exactly $K$ numbers between $1$ and $N - 1$. For example, if $S = 22$, $K = 7$ and $N = 10$, then one possible way is $3 + 3 + 3 + 3 + 3 + 3 + 4 = 22$. Is there any way to construct an algorithm that figures out one possible ways? I have figured out that if $K > S$ or $(N - 1)K < S$, then it won't be possible to do so. But given $K \leq S$ and $(N - 1)K \geq S$, what approach can I take to figure out one of the possible solutions?

Robur_131
  • 181
  • 1
  • 9

1 Answers1

1

If we donote $\lfloor x\rfloor$ the entire part of $x$ and $\{x\}$ the fractional part of $x$, you can let $n=\left\lfloor\frac{S}{K}\right\rfloor,\,k=\left\{\frac{S}{K}\right\}\cdot K$ and take $k$ times $n+1$ and $K-k$ times $n$. $$k\cdot(n+1)+(K-k)\cdot n= K\cdot\left\lfloor\frac{S}{K}\right\rfloor+ \left\{\frac{S}{K}\right\}\cdot K=\frac{S}{K}\cdot K=S$$