Factor Solitaire
Time limit1sMemory limit128 MB
Starting from 1, repeatedly replace c by c + a where a divides c and b = c/a, paying b; find the minimum total cost to reach N.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Number theory, Math, Greedy
- Solved
- No attempts yet
Problem
In the game of Factor Solitaire you start with the number and try to turn it into a given target number by repeatedly applying the following operation.
At each step, let be your current number. Choose two positive factors and with . Add to your current number, so becomes . This step costs you points.
Repeat until your current number equals . Your goal is to reach at the minimum possible total cost.
For example, one way to reach is:
- start with ;
- change to — cost so far ;
- change to — cost so far ;
- change to — cost so far ;
- change to — cost so far ;
- change to — done, total cost .
In fact is the minimum possible total cost to reach . Given a target number, compute this minimum total cost.
Input
The input consists of a single integer with . In at least half of the cases , in at least another quarter of the cases , and in the remaining cases .
Output
Print a single integer: the minimum total cost to reach starting from .