Let represent the minimum number of operations required to transform the number into . For example,
f(1) = 0, as we already have the number ;
f(2) = 1, perform operation 2 → 1;
f(5) = 3, perform operations 5 → 4 → 2 → 1;
f(10) = 3, perform operations 10 → 9 → 3 → 1;
In the case of n = 10, it is more advantageous to subtract first rather than using the greedy approach and dividing by .
Let’s consider the process of computing the function .
If we divide the number by (assuming is divisible by ), then
If we divide the number by (assuming is divisible by ), then
If we subtract from the number , then
From the number , we can obtain one of three numbers: , or n - 1. The number of operations required to reduce each of these numbers to , is equal to f(n / 3), f(n / 2), and f(i - 1) respectively. Since we are interested in the minimum number of operations, we have the following relationship:
In the case, if is not divisible by (or by ), then the corresponding element ( or ) is absent in the function. For example, for n = 8, we have:
Similarly, for n = 7, we get:
The values of the function will be stored in the cells of the array d[MAX], where MAX = 10^6 + 1. Fill the cells of the array from to 10^6 according to the given recurrence relation. For example, the following table shows the values of for 1 \le i \le 11:
For example,
In other words, it is more efficient to subtract from , rather than divide it by .
Exercise
Find the values of for the following values of :
Algorithm implementation
Declare an array , where the -th cell contains the minimum number of operations required to transform the number into .
#define MAX 1000001
int d[MAX];Fill the cells of array according to the provided formulas.
d[1] = 0;
for (i = 2; i < MAX; i++)
{
// d[i] = min(d[i/3],d[i/2],d[i-1]) + 1
d[i] = d[i - 1];
if (i % 2 == 0) d[i] = min(d[i], d[i / 2]);
if (i % 3 == 0) d[i] = min(d[i], d[i / 3]);
d[i]++;
}For each input value , print the answer .
while(scanf("%d",&n) == 1)
printf("%d\n",d[n]);Algorithm implementation — recursion
#include <stdio.h>
#include <string.h>
#define INF 2000000000
int i, res, n;
int dp[1000001];
int min(int a, int b, int c)
{
int res = a;
if (b < res) res = b;
if (c < res) res = c;
return res;
}
int f(int n)
{
if (n == 1) return 0;
if (dp[n] != -1) return dp[n];
int a = f(n - 1);
int b = (n % 2 == 0) ? f(n / 2) : INF;
int c = (n % 3 == 0) ? f(n / 3) : INF;
return dp[n] = min(a,b,c) + 1;
}
int main(void)
{
memset(dp, -1, sizeof(dp));
while (scanf("%d", &n) == 1)
{
res = f(n);
printf("%d\n", res);
}
return 0;
}Java implementation
import java.util.*;
public class Main
{
public static int MAX = 1000001;
static int d[] = new int[MAX];
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
d[1] = 0;
for(int i = 2; i < MAX; i++)
{
d[i] = d[i-1] + 1;
if (i % 2 == 0) d[i] = Math.min(d[i], d[i/2] + 1);
if (i % 3 == 0) d[i] = Math.min(d[i], d[i/3] + 1);
}
while(con.hasNext())
{
int n = con.nextInt();
System.out.println(d[n]);
}
}
}Python implementation
Create a list , where the -th cell contains the minimum number of operations required to transform the number into .
d = [0] * 1000001Fill the cells of array according to the provided formulas.
d[1] = 0
for i in range(2, 1000001):
d[i] = d[i - 1]
if i % 2 == 0 and d[i // 2] < d[i]:
d[i] = d[i // 2]
if i % 3 == 0 and d[i // 3] < d[i]:
d[i] = d[i // 3]
d[i] += 1For each input value , print the answer .
while True:
try:
n = int(input())
print(d[n])
except EOFError:
break