Cake (Greatest Common Divisor)

topic:

A birthday party may have p or q people attending, and a big cake is now ready. Ask at least how many pieces of the cake must be cut (each piece may not be the same size), so that p or q people can attend any situation , Can divide the cake evenly.

Input

Each row has two numbers p and q.

Output

Output the minimum number of cakes to be cut into pieces.

Sample Input

2 3

Sample Output

4


  

Hint

将蛋糕切成大小分别为1/3,1/3,1/6,1/6的四块即满足要求.
当2个人来时,每人可以吃1/3+1/6=1/2 , 1/2块。
当3个人来时,每人可以吃1/6+1/6=1/3 , 1/3, 1/3块。
 

Sponsor

Ideas:

Cut into p parts for the first time, and cut into q parts for the second time. The two must have overlapping cutting edges.

The number of overlapping cutting edges is the greatest common divisor of p and q, and then use (p+q)-greatest common divisor

Implementation code:

#include <stdio.h>
#include <algorithm>
using namespace std;

int main()
{
    int p,q;
    while(~scanf("%d %d",&p,&q))
        printf("%d\n",p+q-__gcd(p,q));
    return 0;
}

supplement:

1. __gcd() function Find the greatest common divisor and call directly, the header file algorithm

Guess you like

Origin blog.csdn.net/with_wine/article/details/113943224