Log inSign up
Problems / B045

Dividing books among bookcases

200 ptsCases 15Set by dongbinna
Added October 30.

Statement

A village library has been donated N books. The librarian has to shelve all of them across K bookcases.

There are two rules to follow. First, the number of books in each bookcase has to differ from the number in every other bookcase. Second, no bookcase may be left empty, so every bookcase has to hold at least one book. On top of that, all N donated books have to be shelved with none left over.

The librarian does not want the bookcases to be too lopsided. The aim is therefore to make the difference between the number of books in the fullest bookcase and the number in the emptiest one as small as possible. There may be no way at all to shelve the books under the rules, and in that case the answer is -1.

Write a program that, given the number of books N and the number of bookcases K, finds the smallest possible difference between the number of books in the fullest bookcase and the number in the emptiest one.

For example, look at N=10 books shelved across K=3 bookcases. There are four ways to split 10 into three counts that all differ and that leave no bookcase empty, as in the following table.

SplitFullest bookcaseEmptiest bookcaseDifference
1, 2, 7716
1, 3, 6615
1, 4, 5514
2, 3, 5523

The smallest difference comes from the split 2, 3, 5, and that difference is 3. The answer in this example is therefore 3.

To shelve N=5 books across K=3 bookcases, on the other hand, the counts all have to differ and all have to be at least one, so at least 1 plus 2 plus 3, that is 6 books, are needed. There are only 5 books, so there is no way to shelve them and the answer in that case is -1.

K can also be 1. With only one bookcase, all N books go into it, and the fullest bookcase and the emptiest one are the same bookcase, so the difference is 0.

Input and Output Examples

Example 1

Input

10 3

Output

3

Example 2

Input

6 3

Output

2

Example 3

Input

5 3

Output

-1

Example 4

Input

1 1

Output

0

Input

  • The number of books N and the number of bookcases K are given first, in that order on the first line, separated by one space. N is a natural number from 1 to 1,000,000, and K is a natural number from 1 to 1,000.

  • This one line is the whole input. Cases where N is smaller than K are given too, and in those cases not even one book per bookcase is possible, so there is no way to shelve the books under the rules.

Output

  • Print the smallest possible difference between the number of books in the fullest bookcase and the number in the emptiest one on the first line. If there is no way to shelve the books under the rules, print -1.

  • Print one number and then a newline. Do not attach any other character or space before or after it.

Requirements

  • Read the input from standard input and write the answer to standard output
  • The number of books in each bookcase has to differ from the number in every other bookcase, and no bookcase may be left empty
  • All N donated books have to be shelved with none left over
  • If there is no way to shelve the books under the rules, print -1
  • There are two numbers to read, N and K on the first line
  • The output is one number and a newline, nothing else
  • Grading compares the output with the expected value down to the last character
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • There are 15 cases, and 4 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

4 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
10 3
3
6 3
2
5 3
-1
1 1
0

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 3.0ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 4 public cases. Submitting runs the hidden cases too.

People who solved this problem

Nobody yet. You could be the first.