You are not logged in. Please login at www.codechef.com to post your questions!

×

NUMFUN - EDITORIAL

PROBLEM LINK:

Practice
Contest

Author: Shivam Wadhwa
Tester: Shivam Manchanda
Editorialist: Shivam Machanda

DIFFICULTY:

EASY

PREREQUISITES:

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.

asked 02 Oct '16, 10:45

torque's gravatar image

6★torque
4271111
accept rate: 17%

edited 02 Jan '17, 18:49

admin's gravatar image

0★admin ♦♦
19.7k350498541

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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