A small conclusion about GCD and LCM

Source: Internet
Author: User

First, we will introduce two:

GCD with large numbers

Stein + Euclidean

  
function stein(a,b:int64):int64;begin      if a<b then exit(stein(b,a));      if b=0 then exit(a);      if ((a and 1)=0) and ((b and 1)=0) then exit(stein(a>>1,b>>1)<<1);      if (a and 1)=0 then exit(stein(a>>1,b));      if (b and 1)=0 then exit(stein(a,b>>1));      exit(stein((a+b)>>1,(a-b)>>1));end;
View code

Decimal GCD

Phase Division

  
function stein(a,b:int64):int64;begin  if a<b then exit(stein(b,a));  if b=0 then exit(a);  if ((a and 1)=0) and ((b and 1)=0) then exit(stein(a>>1,b>>1)<<1);  if (a and 1)=0 then exit(stein(a>>1,b));  if (b and 1)=0 then exit(stein(a,b>>1));  exit(stein((a+b)>>1,(a-b)>>1));end;
View code

 

We often need to calculate the LCM. We have a very elegant conclusion.

A * B/gcd (a, B) = lcm (A, B)

Therefore, we only need to calculate GCD. When A and B are large, it is a good optimization.

Next let's take a look at the question

Description

 

Input

For each test point:
The first row contains an integer T, indicating the number of data groups.
For each group of data that follows, there are two rows.
The first line is an integer n representing the sequence length.
The second line represents the N integers AI separated by spaces, representing the weights calculated for each material.

 

Output

For group I data, you need to specify the number of output groups as "case I:", where I indicates the current number of data groups.
Then, the α and β parameters to be calculated must be output and separated by spaces.
If the required substring does not exist, the corresponding α or β parameter is set to-1.

 

Sample input 327 242 2 3 432 2 4 sample output Case 1: 2 2 Case 2: 4 2 case 3:-1-1 prompt

 

 

This probably means that you need to calculate the two largest substrings respectively, so that gcd (Al, A2 ,...., AR) = 1 lcm (Al ,..... AR) = Al *.... * AR;

GCD is easy to do, and gcd (A [I], a [I + 1]) is continuously read. If gcd (A [I], a [I + 1]) exists, = 1 indicates the interlace, that is, Ans: = N; otherwise, no solution is available.

2. DP practices

F [I] = max (F [I-1] + 1, I-k + 1); k is the number of the last number not equal to the number of ai counterparts.

The answer is max (F [1]..., F [n-1], F [N]);

Complexity O (N)

Solution 2: Maintain the queue
1. maintain such a queue to ensure the mutual quality of the queues.

2. Enable elements to join the queue from left to right. If no mutual quality exists in the queue, enable the first line of the queue until the mutual quality is met. In this process, record the number of elements.

   

 

 

A small conclusion about GCD and LCM

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.