Log inSign up
Problems / B099

Cutting down the water in the tank

200 ptsCases 15Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

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.

OperationWhat it doesWhen it can be used
Operation 1Cuts the water down to a thirdWhen the current amount is divisible by 3
Operation 2Cuts the water down to a halfWhen the current amount is divisible by 2
Operation 3Pours out 1 literAlways

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.

StepStartAfter the 1st operationAfter the 2nd operationAfter the 3rd operation
Operation usedoperation 3operation 1operation 1
Amount of water10931

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.

StepStartAfter the 1st operationAfter the 2nd operationAfter the 3rd operationAfter the 4th operation
Operation usedoperation 2operation 3operation 2operation 2
Amount of water105421

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.
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

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

Input
Expected output
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.

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

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.

1bampal21.66s265 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.