vijos-p1279leave-green light (mathematical induction + python)

Source: Internet
Author: User
Tags gcd greatest common divisor

P1279leave-Green light accepted Label: [Show label]<textarea id="code" class="textbox" style=""></textarea>Background

Looking forward to this luck, and a momentum, what a wonderful fate .... Stef in the green light of the first songs, gave a pose fan an exam.

There is a legend in northern Europe!
A man can see green light in his life!
He can get happiness all his life!

Describe

Stef sang this song, the sky landed a green light, on the ground formed a rectangular map, the length of the rectangle is a, width is b. Stef A question, who can put this rectangular green light array into a number of positive integer square, whose square Bengchang and the smallest, he will get a photo of Stef. Attitude fans are very interested in getting a group photo, but how to divide the smallest? Everyone is helpless, now, this question to you.

Fan x: Woo, my Chinese is not good, I do not understand what you are talking about.

Stef: Don't be afraid, actually this problem can be simplified as ...

The rectangle of the side length as a positive integer, A, a, a, or a number of sides is a positive integer, the edges of each square are parallel to the corresponding sides of the rectangle, and the minimum min of the sum of the squares is tried.

(If the rectangle can be divided into n squares, where each side is an AI, then min=a1+a2+ ^^ ^+an
Note that the elements in array a may be equal)

Format input Format

10 lines altogether.
Two positive integers per line, Ai,bi

For 30% of data, Ai,bi<maxint
For 100% of data,ai,bi<maxlongint;

Output format

10 lines altogether.
One integer per line, output mini

Example 1 sample input 1[copy]
1 12 13 14 15 16 17 18 19 110 1
Sample output 1[Copy]
12345678910
Limit

1s per point

Tips

For the sample, you can divide the length of the side length into a square, and

Remember that the minimum value is f (m,n), can prove F (m,n) =m+n-(m,n). (*)
Where (M,n) represents the greatest common divisor of M and N.
In fact, it is advisable to set m≥n.
(1) About M induction, it can be proved that there is a kind of test instructions, so that the resulting square Bengchang and just m+n-(m,n).
When M=1, the proposition is clearly established.
Assuming that when m≤k, the conclusion is established (k≥1). When the m=k+1, if n= k+1, then the proposition is clearly established. If n< k+1, cut a square from a rectangular abcd to a side length of N (),


It is assumed by induction that the remainder of the rectangle has a sub-method that makes the resulting square Bengchang and exactly m-n+n-(m-n,n) = m (m,n).
So the original rectangle ABCD has a kind of sub-method to make the resulting square Bengchang and m+n-(m,n).
(2) about M induction can be proved (*) to be established.
When M=1, due to n=1, obviously F (m,n) =1= m+n-(m,n).
When M≤k is assumed, there is f (m,n) = m+n-(m,n) for any 1≤n≤m.
If m=k+1, when n= k+1, obviously f (m,n) = k+1= m+n-(m,n).
When 1≤n≤k, set the rectangle ABCD according to the requirements of the P-square, its edge length is A1,A2,..., ap, may wish to set a1≥a2≥ ... ≥ap.
Apparently a1=n or A1 if A1 m+n-(m,n).
If A1=n, a rectangle with an edge length of M-n and n can be divided into a square of A2,..., AP according to the topic requirement, which is assumed by induction
a2+...+ap≥m-n+n-(M-n,n) = m (m,n).
thereby a1+a2+...+ap≥m+n-(M,n).
So when M=k+1, F (m,n) ≥m+n-(m,n).
Again by (1) know F (m,n) =m+n-(m,n).


#!/usr/bin/env python3#-*-coding:utf-8-*-def gcd (A, b):    if not B:        return a    else:        return gcd (b, a% b) Def LCM (A, B):    return a + B-GCD (A, b) import Sysimport mathfor i in range:    A, B = Map (Int,raw_input (). Split () )    print LCM (A, B)    


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

vijos-p1279leave-green light (mathematical induction + python)

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.