Sicily 10330. Cutting sausages

Source: Internet
Author: User
Tags greatest common divisor

10330. Cutting sausages Constraints

Time limit:1 secs, Memory limit:256 MB

Description

Mirko have given up on the difficult coach job and switched to food tasting instead. Had skipped breakfast like a professional connoisseur, he was visiting a Croatian cured meat festival. The most renowned cook at the festival, Marijan Bajs, have prepared N equal sausages which need to being distributed to M tast ERs such that each taster gets a precisely equal amount. He'll use his trusted knife to cut them-pieces. in order to elegantly divide the sausages, the number of cuts Splitting individual sausages must be as small as possible. For instance, if there is both sausages and six tasters (the first test case below), it's sufficient to split each SAUSAG E into three equal parts, making a total of four cuts. On the other hand, if there be three sausages and four tasters (the second test case below), one possibility is cutting o FF three quarters of each sausage. Those larger parts would each go to one of the tasrers, while the fourth taster would get the three smaller pieces (quArters) left over. Mirko wants to try the famous sausages, so he volunteered to help Bajs. Help them calculate the minimum total number of cuts needed to carry out the desired division.  

Input

The first and only line of input contains, positive integers, N and M (1≤n, m≤100), the number of sausages and tast ERS, respectively.

Output

The first and only line of output must contain the required minimum number of cuts.

Sample Input
Example 1:2 6 Sample 2:3 4 Example 3:6 2
Sample Output
Sample 1:4 Sample 2:3 Example 3:0
Problem Source

COCI 2013.9/2014 Annual first game of the week


It is worthwhile to think of a topic that has been made possible by reference to the information of the Great God:

Analysis:


Look at the above diagram is obvious, you can see people as a section of the line, sausage is the same, and then, the minimum number of knives is to try to make the point between the end of the person and the sausage between the breakpoint (not with a knife cut) coincident;

Then, the number of points that are not coincident on the line of the person is the fewest knives.

How do you find the number of points not coincident? First the M-person has m + 1 endpoints, but the endpoints on both ends are not considered, that is, only consider m-1 endpoints.

Set each person's length to Y, each sausage length is x, then first there is x * n = m * y;

After I have a person and J sausage will have an endpoint coincident, that is I * y = j * x;

So the number of people inside is (m * y)/( i * y x * n )/( j * x );

The numerator denominator is the same, indicating that the number of people inside and inside the sausage is the same;

Eliminate the x and Y in the formula, get: m/i = n/j;

Set them equal to K;

Then K is obviously the public approximate of M and N;

And in order to make the breakpoint between the person and the sausage as much as possible, we have to make I and j as short as possible, then by m/k = i and n/k = J know, K will be as large as possible;

So k is the greatest common divisor of m,n;

so again ( m * y)/( i * y ) = K and ( x * n )/( j * x

Then cut the minimum number of knives, that is, the non-coincident consideration of the points m-1 minus the number of coincident points considered (GCD (n, m)-1);

Code:

#include <stdio.h>int gcd (int a, int b) {    if (b = = 0)        return A;    else        return gcd (b, a% b);} int main () {    int n, m;    scanf ("%d%d", &n, &m);    printf ("%d\n", M-1-(GCD (n, m)-1));    return 0;}                                 

Sicily 10330. Cutting sausages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.