GCD and Euclid's algorithm


Plus
Pin


Problem description Progress
ID 18692. Long gcd
Темы: recursion    GCD and Euclid's algorithm   

Two numbers are given. Find their greatest common divisor.
 
Input data: Enter two natural numbers not exceeding 10^9, (record 10^9 means "10 to the 9th power", that is, 1000000000).< /div>
Output: Print the GCD of the entered numbers

Examples
# Input Output
1 42 12 6

ID 30770. Single GCD
Темы: GCD and Euclid's algorithm   

Two natural numbers in the decimal number system, consisting of ones, are given. The first number has exactly N units, and the second has exactly M. It is required to find the GCD of these numbers. 
 
Input
In a single line  two integers N and M (\(1 <= N,\ M <= 2000\)).
 
Output
Print your answer without leading zeros.
 

 

Examples
# Input Output
1 1 1 1
2 1 2 1

ID 37550. Father and daughter
Темы: GCD and Euclid's algorithm   

Dad step length – And see, and the little daughter's – In cm. They begin to walk, putting their feet on one mark. How far will they walk before their feet are level again?

Write the solution to the problem as a function solve(A, B) that returns the answer. You don't need to enter or withdraw anything!

Examples

# Input Output
1 70 15 210

ID 37551. Math competition
Темы: GCD and Euclid's algorithm   

At the mathematical competition, the guys played the fascinating ancient Chinese puzzle Tangram. In one game set there were A acute-angled triangles, and in the other - B obtuse-angled triangles. What is the smallest number of participants who can use sets of the same number of each type of triangles?

Write the solution to the problem as a function solve(A, B) that returns the answer. You don't need to enter or withdraw anything!

Examples

# Input Output
1 12 15 60

ID 37552. orchids
Темы: GCD and Euclid's algorithm   

In the greenhouse, they planted different varieties of orchids in 2 rows. Flowers of one variety were placed at a distance of A cm between plants, and the other - at B cm. How far will the orchids of both varieties be next to each other? 

Write the solution to the problem as a function solve(A, B) that returns the answer. You don't need to enter or withdraw anything!

Examples

# Input Output
1 15 18 90

ID 37553. Amount of information
Темы: GCD and Euclid's algorithm   

A student in his first year downloaded an average of A Kb of information per day from the Internet, and in his second year – In Kbytes, paying the same amount of money for the received Internet traffic per week. What is the smallest amount of information in Kbytes he downloaded in a week?

Write the solution to the problem as a function solve(A, B) that returns the answer. You don't need to enter or withdraw anything!

Examples

# Input Output
1 60 75 300

ID 37554. Vietnamese craftsmen
Темы: GCD and Euclid's algorithm   

Vietnamese folk craftsmen make decorative panels from pieces of rice straw, sticking them on boards. What is the smallest length (in mm) that the straw must be so that it can be cut into equal parts along A mm and B mm without cutting?

Write the solution to the problem as a function solve(A, B) that returns the answer. You don't need to enter or withdraw anything!

Examples

# Input Output
1 20 27 540

ID 21815. NOD and NOC
Темы: GCD and Euclid's algorithm   

Seryozha loves math problems very much. Recently, at a mathematical circle, he was told what GCD and NOC are. 
gcd of two natural numbers a and b — is their greatest common divisor, that is, the maximum number x such that a is divisible by x and b is divisible by x. For example, \(gcd(24, 18) = 6\). And the LCM of integers a and b — is their least common multiple, that is, the minimum number x such that x is divisible by a and x is divisible by b. For example, \(LCC(24, 18) = 72\).
Seryozha immediately noticed that there can be several pairs of numbers with the same GCD and LCM. Now he was interested in the question: given the numbers a and b, how close can two numbers be that have the same gcd and lcm.
Help him given two numbers a and b to find numbers x and y such that \(gcd(a, b) = gcd(x, y)\), \(gcd(a, b) = gcd (x, y)\) and their difference \(y - x\) is minimal. 

Input 
The first line of the input file contains two natural numbers a and b (\(1 <= a, b <= 10 ^9\)).
 
Output data 
Print two natural numbers x and y (\(1 <= x <= y\)) , such that \(gcd(a, b) = gcd(x, y)\)\( LCM(a, b) = LCM(x, y)\) and their \(y - x\) difference is minimal.
 
Examples
# Input Output
1 3 4 3 4

ID 12482. ordered fractions
Темы: GCD and Euclid's algorithm   

Print in ascending order all irreducible fractions between 0 and 1 whose denominators do not exceed N.

Input 
The first line contains a single number N (\(2 <= N <= 255\)).

Imprint 
One fraction is displayed per line.
 
Examples
# Input Output
1 5 1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5

 

ID 30777. oranges
Темы: GCD and Euclid's algorithm   

Katya decided to invite n friends to visit her. Since her friends are very fond of fruits, she bought m identical oranges as a treat for them. She wants to cut each orange into the same number of equal slices so that they can be distributed among the guests (Katya herself will not eat oranges), and everyone gets the same number of slices.

Write a program that calculates the minimum number of slices each orange must be cut into to satisfy the above conditions.
 

Input 
The input string contains two positive integers n and m (\(1 <= n, m <= 10^9 \)).

Imprint 
Print the answer to the problem.
 
Examples
# Input Output
1 2 5 2
2 2 4 1

ID 12483. squares
Темы: GCD and Euclid's algorithm   

At the labor lesson, everyone was given a rectangle with sides measuring A and B (whole, \( 1 <= A, B <= 2^{31} - 1\)). The boy Senya is very fond of cutting rectangles with special cynicism, and when the teacher invites everyone to cut out squares from a rectangle, Senya acts very cunningly. With one cut parallel to the side of the rectangle, he cuts off from the rectangle a square with a side equal to the smallest side of the rectangle and continues to do the same procedure with the part remaining after the cut. If a part turns out to be a square, then Senya calms down and begins to count the resulting squares.
How many squares will he cut?

Input
The numbers A and B are specified on the same line separated by a space.

Imprint
The number of resulting squares.
 


Examples
# Input Output
1 1 2 2

ID 31872. Add two fractions
Темы: GCD and Euclid's algorithm   

Given two rational fractions: \(a \over b\) and \(c \over d\)< /span>. Add them up and write the result as an irreducible fraction \(m \over n\).
 
Input 
The program receives as input 4 natural numbers a, b, c, d, not exceeding 100.
 
Output 
The program should output 2 natural numbers m and n such that \({m \over n} = {a \over b }+ {c \over d}\) and fraction \(m \over n\) – irreducible.
 
Examples
# Input Output
1  1 3 1 2 5 6

ID 29423. Fraction reduction
Темы: GCD and Euclid's algorithm   

Given a fraction \(a \over b\). It is required to reduce it, that is, write the same number in the form \(c \over d\), where c — integer, d is a natural number and d is the smallest possible.
 
Input 
Enter two integers a and b (\(-100<=a<=100,\ 0<b<=100 \)).

Imprint 
Output two numbers c and d.
 
Examples
# Input Output
1 3 6  1 2
2 -2 5 -2 5

ID 34852. Long gcd
Темы: GCD and Euclid's algorithm   

Two numbers are given. Find their greatest common divisor.
 
Input data 
Two natural numbers not exceeding 109 are entered.

Imprint 
Output the GCD of the entered numbers.
 

Examples
# Input Output
1 42 12 6

ID 18691. Short GCD
Темы: GCD and Euclid's algorithm   

Two numbers are given. Find their greatest common divisor.
 
Input 
Two natural numbers not exceeding 30000 are entered.
 
Imprint 
Output the GCD of the entered numbers.
 
 
Examples
# Input Output
1 42 12 6
 

ID 24905. NOD and NOC
Темы: GCD and Euclid's algorithm   

Given a number n – amount of numbers. The next line contains n numbers, each not greater than 1000.
You need to output the number of pairs of numbers (a, b) such that LCD (a, b) = gcd (a, b).

LCM (a, b) is the least common multiple of these two numbers, that is, the smallest number that is divisible by both numbers at once. \( LCM (20 , 30) = 60\).
GCD (a, b) – the greatest common divisor of these two numbers, that is, the largest number that both numbers are divisible by. \(gcd (20, 30) = 10\).
Write a memory and time efficient program.

Input
The first line contains a natural number n – the number of numbers given to you.
The second line contains the numbers themselves, each of them is an integer and belongs to the segment [0; 1000].
 
Output
Print a single integer – number of pairs (a, b) such that LCC(a,b) = gcd(a,b).
 

 

Examples
# Input Output
1 3
3 3 3
3

 

ID 38258. Stew
Темы: GCD and Euclid's algorithm   

Summer was approaching, and Igor, Gena and Denis decided to go camping together, just like last year. Almost all issues have already been resolved: the route has already been worked out, train tickets have been bought, a four-seater tent has been found in Denis’s closet, and under Igor’s bed — axe. It remains only to solve the issue with the products.

Last year, the guys honestly admitted that they did not know how much food they needed, and asked their classmate Nastya to help, who made a shopping list for them. But this year, asking her again is somehow undignified. One could look at how much they bought last year and take the same amount, but none of the guys had any receipts or a grocery list.

But they still have correspondence on the social network, where they argued over who would carry how many cans of stew. In this correspondence, Igor first suggested dividing the whole canned meat in relation to a: b: c, so that Igor himself would bear the first part, the second — Gena, and the third — Denis. But Denis did not like it, and he suggested changing the ratio to d: e: f.

After some more thinking, the guys realized that they still wouldn’t cut the cans of stew, and, therefore, both ratios were chosen so that each in both cases got an integer number of cans.

Given two ratios a: b: c and d: e: f, calculate how many cans of stew the guys bought for last year's trip. From all possible answers print the minimum. The guys remember that they definitely had at least one can of stew.

Input
The first line contains three positive integers a, b, c separated by spaces. The second line contains three positive integers d, e, f, also separated by spaces.

All numbers are less than 1000.

Imprint
Print a positive integer — number of cans of stew.
 

Examples
# Input Output Explanations
1 10 3 7
3 1 1
20 When dividing the stew between the guys in a ratio of 10:3:7, Igor will incur 10 cans, Gena — 3 cans, and Denis — 7 cans. And in the case of a ratio of 3:1:1, Igor will get 12 cans, and Genya and Denis will get 4 each.

ID 38300. Buses
Темы: GCD and Euclid's algorithm   

Berland has a deplorable situation with intercity bus service. There are only three bus routes in the whole country, each of which runs only one bus. On the first day of the new year, at exactly midnight, all three buses depart on their routes from the capital of Berland. It is known that the first bus needs a minutes to cover the whole route and return to the capital, the second — b minutes, and the third — c minutes. Thus, the first bus departs from the capital of Berland at times 0, a, 2a, 3a, the second — at times 0, b, 2b, 3b , and the third at times 0, c, 2c, 3c .

The moment of time is called suitable for transfer if at this moment all three buses depart from the capital of Berland. For example, if a=1, b=2, c=1, then times 0 and 2 are eligible for transfers, but time 1 is not, because at that time a second bus is on the way. Berland — a special country with a special measurement of time, so there are exactly t minutes in a Berland day. This means that on the first day all moments of time from 0 to (t & minus; 1) inclusive occur, on the second day — from the t-th to (2t−1)-th inclusive, to the third — from 2t to (3t−1) inclusive and so on.

The Ministry of Transport of Berland was interested in how many suitable time points for a transfer will occur on the d-th day in Berland. Unfortunately, local officials are busy with other matters, so you have been assigned to answer this question.

Input
Five lines contain five integers a, b, c, t  and d (1≤a, b, c≤106 , 1≤t, d≤109 ) — the time of the complete passage of the route by the first, second and third buses, respectively, the number of minutes in a day and the number of the day, which is of interest to the Ministry of Transport of Berland.

Imprint
Print a single integer — the number of times suitable for transplantation on the d-th day.

Examples
# Input Output Explanations
1 1
2
1
3
1
2 A day lasts 3 minutes, so all times on a day with number 1 — it 0, 1, 2, of which 0 and 2 times are eligible for transplant
2 2
3
4
7
2
1 Here we consider the second day with time points 7, 8, 9, 10, 11, 12, 13. The first bus leaves at times 8, 10, 12, the second bus — at times 9 and 12, and the third — at times 8 and 12. Thus, only time 12 is suitable for transplantation.
3 2
3
4
3
3
0 There is no suitable time to transplant

ID 38323. Segment_0
Темы: GCD and Euclid's algorithm   

On checkered paper, Petya drew a segment from a point with coordinates (a,b) to a point with coordinates (c,d). How many cells does this segment pass through?
Input
Integers a, b, c, d are entered. Modulo numbers do not exceed 109.

Imprint
Print one number — the number of cells through which the segment passes.

Examples
# Input Output
1 0 0 6 4 8
2 3 3 -3 3 0

ID 38432. Integer points
Темы: Elementary geometry    GCD and Euclid's algorithm   

A polygon (not necessarily convex) on a plane is given by the coordinates of its vertices. It is required to count the number of points with integer coordinates lying inside it (but not on its border).

Input data format
    The first line contains N (3 ≤N ≤1000) – the number of vertices of the polygon. The next N lines contain the coordinates (Xi, Yi) of the polygon vertices in clockwise order. Xi and Yi are integers, modulo not exceeding 1000000.

Output data format
Output a single number – desired number of points.

Examples
# Input Output
1 4
-1 -1
-1 1
1 1
1-1
1
2 3
0 0
0 2
20
 
0

ID 38584. Many hours
Темы: GCD and Euclid's algorithm   

Gromozeka has N hours. The ith clock hand (\(1<=i<=N\)) is rotated 360 ° exactly Ti seconds. Initially, the hand of all clocks stands still and points straight up. Gromozeka starts all the clocks at the same time. After how many seconds will the hand of all clocks point straight up again?

Input
The first line contains an integer N (\(1<=N<=100\)). The following lines contain integers Ti (\(1<=T_i<= 10^{18}\)), one number per line. 

Imprint
Display the answer. It is guaranteed that the answer does not exceed \(10^{18}\).
 

 

Examples
# Input Output Explanation
1 2
2
3
6 We have two clocks. The time when the hand of each clock points up is as follows:
Clock 1: 2, 4, 6, ... seconds after start.
Clock 2: 3, 6, 9, ... seconds after start.
So it takes 6 seconds until the hands of both clocks point straight up again.
2 5
2
5
10
1000000000000000000
1000000000000000000
1000000000000000000  

 

ID 38847. Cinema
Темы: GCD and Euclid's algorithm   

Marya Ivanovna and Marya Mikhailovna led the schoolchildren to the cinema. So that there would be no offense, Marya Ivanovna lined up all the schoolchildren in alphabetical order and seated them: first in the first row from left to right, then in the second from left to right, etc., filling the entire hall of n rows of m seats. Then Marya Mikhailovna came and said that the guys sat down incorrectly – need to transfer. She suggested first filling all the first places from the first row to the last, then all the second places, and so on.

Determine how many schoolchildren will remain in their place after such a transplant.

For example, if n = 3 and m = 3, then in the first case the children will sit like this:

1    2    3
4    5    6
7    8    9
and in the second – like this:
1    4    7
2    5    8
3    6    9
Thus, three students: 1, 5 and 9 will remain in their places.

Input
Enter two integers n and m (1 ≤ n, m ≤ 109 ).

Imprint
Output the number of schoolchildren who will remain in their places.
 

Examples
# Input Output
1 3 3 3
2 2 4 2

ID 38877. Greatest Common Divisor
Темы: GCD and Euclid's algorithm   

The greatest common divisor of a non-empty collection of natural numbers A is the maximum natural number d such that it is simultaneously a divisor of all numbers in the set A.
Given an array of natural numbers [a1, a2, . . . , an] and number k. It is required to choose a subarray of k consecutive elements [al, al+1, . . . , al+k−1] so that their greatest common divisor is as large as possible, and output this greatest common divisor.

Input
The first input line contains two integers n and k (2 ≤ n ≤ 500 000, 2 ≤ k ≤ n).
The second line contains n natural numbers a1, a2, . . . , an (1 ≤ ai ≤ 1018).

Imprint
Print one natural number — the maximum possible value of the greatest common divisor of the elements of a subarray of length k of the given array.

Examples
# Input Output
1 10 4
2 3 4 8 12 6 12 18 4 3
6

ID 39392. Walk Gromozeka and Alice
Темы: GCD and Euclid's algorithm   

Gromozeka and Alice are old friends. When they meet on some planet, they constantly go to a cafe. But Alice doesn't like to go to every a cafe, and Gromozeka doesn't like to go to every g cafe. In order not to offend anyone, they do not go to those cafes that Alice and Gromozeka do not want to go to at the same time. On their next walk, they have a N cafe on their way. How many cafes can they go to?

Input
The only line contains three integers - a , g , N ( 1 <= a , g , N <= 10 9 ).

Imprint
Print a single integer - the number of cafes that Gromozeka and Alice can visit.
 

Examples
# Input Output
1 1 1 10 0
2 1 2 5 3

ID 43128. Regular operations on an array
Темы: GCD and Euclid's algorithm   

It was a normal weekday evening in Magnitogorsk. Phil and Cosmos were returning home by car after a hard shift. Here Kosmos remembered that Bely had given him a task that he had safely forgotten to complete. To protect Kosmos from the wrath of Sasha Bely, help him complete the task.
Given n integers a1,a2,...,an. It is required to make the greatest common divisor (GCD) of all numbers in the array equal to 1. In one operation, you can do the following:
     Choose an arbitrary index in array 1 <= i <= n;
     Make ai = gcd(ai,i). The cost of such an operation is n − i + 1.
It is required to find the minimum total cost of operations that will need to be done so that the GCD of the array numbers becomes equal to 1.
Input
Each test consists of several sets of input data. The first line contains an integer t (1 <= t <= 5000) — the number of test cases. The following is a description of the input data sets.
The first line of each test case contains a single integer n (1 <= n <= 20) — the length of the array.
The second line of each test case contains n integers a1,a2,...,an (1 <= a< sub>i <= 109) — array elements.
Imprint
For each test case print a single integer — the minimum total cost of operations that will need to be done so that the GCD of the array numbers becomes equal to 1.
 

Examples
# Input Output
1 7
1
1
1
2
2
24
3
3 6 9
4
5 10 15 20
5
120 60 80 40 80
6
150 90 180 120 60 30
0
1
2
2
1
3
3


Remark
In the first test case, the GCD of the entire array is already equal to 1, so there is no need to apply the operation.
In the second test case, choose i = 1. After this operation, a1 = gcd(2,1) = 1. The cost of this operation was 1.
In the third test case, you will need to choose i = 1, after which the array a will be equal to [1,4]. The gcd of this array is 1 and the total cost is 2.
In the fourth test case, you need to choose i = 2, after which the array a will be equal to [3,2,9]. The gcd of this array is 1 and the total cost is 2.
In the sixth test case, you can choose i = 3, after which the array a will be equal to [120,60,1,40,80]. The gcd of this array is 1 and the total cost is 3.
 

ID 44036. Balance gifts
Темы: GCD and Euclid's algorithm    Prime numbers and factorization   

Santa Claus has N gift bags. Each bag has a weight of a1, a2, ..., < code>aN. For a uniform load on the sleigh, Santa Claus needs the weight of all bags to be the same. To achieve this, Santa Claus with his magic staff can perform one of the following operations any number of times, possibly zero times.

  • Santa Claus can choose any bag and if its weight is a multiple of two, then reduce the weight by half.
  • Santa Claus can choose any bag and if it is a multiple of three, then reduce the weight by three times.

Santa Claus takes 1 second for each operation.

Find the minimum total number of seconds it takes Santa Claus to reach the same weight of all the gift bags. If there is no way to achieve the goal, print the value -1.

instead

Input
The program takes as input in the first line an integer N (2 <= N <= 1000). The second line contains N numbers ai - the weight of the i-th gift bag (1 <= < code>ai <= 109).


Imprint
Print the answer to the problem.
 
 
Examples
# Input Output
1 3
1 4 3
3
2 3
2 7 6
-1
3 6
1 1 1 1 1 1 
0