Cutting down the water in the tank
Statement
A laboratory has one tank with water in it. The tank holds exactly N liters of water right now.
The researcher wants to leave exactly 1 liter of water in this tank. The researcher has only the following three operations.
| Operation | What it does | When it can be used |
|---|---|---|
| Operation 1 | Cuts the water down to a third | When the current amount is divisible by 3 |
| Operation 2 | Cuts the water down to a half | When the current amount is divisible by 2 |
| Operation 3 | Pours out 1 liter | Always |
Every time one operation is carried out the count goes up by 1. The researcher wants to bring the tank down to 1 liter with as few operations as possible.
Given the amount of water N held at the start, write a program that calculates the smallest number of operations needed to bring the water down to exactly 1 liter.
For example, suppose N=10. The operations below reach 1 liter in three steps.
| Step | Start | After the 1st operation | After the 2nd operation | After the 3rd operation |
|---|---|---|---|---|
| Operation used | operation 3 | operation 1 | operation 1 | |
| Amount of water | 10 | 9 | 3 | 1 |
Starting with operation 2 to cut 10 liters down to 5 liters is another idea. But 5 liters is divisible by neither 2 nor 3, so operation 3 has to make it 4 liters, and 2 liters and then 1 liter follow, which takes four steps in all. Writing this out as a table gives the following.
| Step | Start | After the 1st operation | After the 2nd operation | After the 3rd operation | After the 4th operation |
|---|---|---|---|---|---|
| Operation used | operation 2 | operation 3 | operation 2 | operation 2 | |
| Amount of water | 10 | 5 | 4 | 2 | 1 |
Four is more than three, so it is not a good way. There is no way that takes fewer than three steps, so the answer in this example is 3.
It is worth pointing out that picking the operation which cuts the water down the most right now is not always best. The operation that cuts 10 liters down to a third cannot be used, so cutting it in half looks like the better move, but pouring out 1 liter first to make 9 liters actually takes fewer steps.
Input and Output Examples
Example 1
Input
10
Output
3
Example 2
Input
1
Output
0
Example 3
Input
1000000
Output
19
Input
First the amount of water N held in the tank is given on the first line. N is a natural number from 1 to 1,000,000 and the unit is liters. The input ends with this one line. The kinds of operations that can be used are not given in the input, they are always the three written out above.
Output
Print on one line the smallest number of operations needed to bring the water in the tank down to exactly 1 liter. If the tank holds 1 liter from the start, no operation is needed, so print 0.
Requirements
- The operation that cuts the water down to a third can be used only when the current amount is divisible by 3
- The operation that cuts the water down to a half can be used only when the current amount is divisible by 2
- The operation that pours out 1 liter can always be used
- The goal is exactly 1 liter. The water may not be brought below 1 liter
- 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.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 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.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1
0
10
3
1000000
19
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.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.