×

# NUMFUN - EDITORIAL

Tester: Shivam Manchanda
Editorialist: Shivam Machanda

EASY

DP , 2D-DP

# QUICK EXPLANATION:

For every pair of N and M there are atmost 3 ways to proceed. $Decrease N$, or $decrease M$ or $N/=GCD(M,N) and M/=GCD(M,N)$.

# EXPLANATION:

Make a 2-D array DP, where DP[i][j] represents minimum ways to make i and j both 1. You can now make a recursive definition:

DP[1][1]=0;
for(i from 1 to N)
{
for(j from 1 to M)
{
if(i==1 && j==1)
continue;
if(gcd(i,j) != 1)
minimum from (DP[i/gcd][j/gcd]+1 , DP[i-1][j]+1 , DP[i][j-1]+1 );
else
minimum from ( DP[i-1][j]+1 , DP[i][j-1]+1 );
}
}


# Time complexity

$O(N*M)$

# AUTHOR'S AND TESTER'S SOLUTIONS:

Solution can be found here.

6★torque
4271111
accept rate: 17%

19.7k350498541

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,482
×3,706
×2,086
×9
×3

question asked: 02 Oct '16, 10:45

question was seen: 540 times

last updated: 02 Jan '17, 18:49