2020.1.7 school test points T1 ham

Topic Link   https://www.luogu.com.cn/problem/U101928

 

T1 looked at that very water, turned out I was wrong, tragic explosion zero qwq. While it is true quite water

Ah, first of all look at the topic in detail:

The ham is divided into n number of equal parts of m , m is not para I died here 

In other words, you can put the ham is divided into two: 2 / 3,1 / 3,1 / 3,2 / 3 four sections, which can be regarded as a second and third paragraphs!

answer

That being the case, we simply put the ham n roots as a large root good;

If we assume that the length of each ham is 1, the length of which is n-root hams;

We want to split into equal parts by m, then it is the length of every n / m;

Given our multi-knife to cut some, so we need to cut up to m-1 knife;

Also need to pay attention to one place:

This m-1 knife in several ham knife is probably the interface, that had this place is broken, just as our man is a piece of it, but we took a knife to cut it.

So we just need to find it again m-1 the number of knives in a knife just cut in two at the interface of ham, with a minus m-1 is the last answer.

Since there is conceivable a knifed in the two interfaces of ham, then the number of divided front section length of n / m may be composed of several sub-root put together complete ham;

That this interface must meet is the length of each of ham (1) and each of a length of a multiple (n / m) multiples, had a positive integer;

Since several parts by a number of preceding ham cut to complete the root, and at the same time must ensure Talia integer multiple;

Then we start out in claim lcm (1, n / m) to.

设 k = lcm ( 1,n/m ) = lcm ( m,n ) / m = m * n / gcd ( n,m ) / m = n / gcd ( n,m )

Hams then the length n have n / k = n / (n / GCD (n, m))  multiples = gcd (n, m) k-a, i.e. there are so many interfaces to meet the requirements;

However, due to a multiple of n it is k, so we put the final cut (knife cut in the length n that big ham end) to count, in fact, is not needed, you need to minus one, that is:

gcd ( n,m ) - 1

Finally, we then subtract a m-1, is the final answer:

Ans = ( m - 1 ) - ( gcd ( n,m ) - 1 ) = m - gcd ( n,m )

Then we direct output m - gcd (n, m) and you're done!

Code:

#include<iostream>
#include<cstdio>
using namespace std;
int T,n,m;
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        printf("%d\n",m-gcd(n,m));
    }
}

Guess you like

Origin www.cnblogs.com/xcg123/p/12165828.html