Knowee
Questions
Features
Study Tools

For k๐‘˜ positive integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜, the value gcd(x1,x2,โ€ฆ,xk)gcd(๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜) is the greatest common divisor of the integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜ย โ€” the largest integer z๐‘ง such that all the integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜ are divisible by z๐‘ง.You are given three arrays a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›, b1,b2,โ€ฆ,bn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› and c1,c2,โ€ฆ,cn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› of length n๐‘›, containing positive integers.You also have a machine that allows you to swap ai๐‘Ž๐‘– and bi๐‘๐‘– for any i๐‘– (1โ‰คiโ‰คn1โ‰ค๐‘–โ‰ค๐‘›). Each swap costs you ci๐‘๐‘– coins.Find the maximum possible value ofgcd(a1,a2,โ€ฆ,an)+gcd(b1,b2,โ€ฆ,bn)gcd(๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›)+gcd(๐‘1,๐‘2,โ€ฆ,๐‘๐‘›)that you can get by paying in total at most d๐‘‘ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the q๐‘ž possible values d1,d2,โ€ฆ,dq๐‘‘1,๐‘‘2,โ€ฆ,๐‘‘๐‘ž.InputThere are two integers on the first lineย โ€” the numbers n๐‘› and q๐‘ž (1โ‰คnโ‰ค5โ‹…1051โ‰ค๐‘›โ‰ค5โ‹…105, 1โ‰คqโ‰ค5โ‹…1051โ‰ค๐‘žโ‰ค5โ‹…105).On the second line, there are n๐‘› integersย โ€” the numbers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1081โ‰ค๐‘Ž๐‘–โ‰ค108).On the third line, there are n๐‘› integersย โ€” the numbers b1,b2,โ€ฆ,bn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰คbiโ‰ค1081โ‰ค๐‘๐‘–โ‰ค108).On the fourth line, there are n๐‘› integersย โ€” the numbers c1,c2,โ€ฆ,cn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰คciโ‰ค1091โ‰ค๐‘๐‘–โ‰ค109).On the fifth line, there are q๐‘ž integersย โ€” the numbers d1,d2,โ€ฆ,dq๐‘‘1,๐‘‘2,โ€ฆ,๐‘‘๐‘ž (0โ‰คdiโ‰ค10150โ‰ค๐‘‘๐‘–โ‰ค1015).OutputPrint q๐‘ž integersย โ€” the maximum value you can get for each of the q๐‘ž possible values d๐‘‘.ExamplesinputCopy3 41 2 34 5 61 1 10 1 2 3outputCopy2 3 3 3 inputCopy5 53 4 6 8 48 3 4 9 310 20 30 40 505 55 13 1000 113outputCopy2 7 3 7 7 inputCopy1 13450outputCopy7

Question

For k๐‘˜ positive integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜, the value gcd(x1,x2,โ€ฆ,xk)gcd(๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜) is the greatest common divisor of the integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜ย โ€” the largest integer z๐‘ง such that all the integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜ are divisible by z๐‘ง.You are given three arrays a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›, b1,b2,โ€ฆ,bn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› and c1,c2,โ€ฆ,cn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› of length n๐‘›, containing positive integers.You also have a machine that allows you to swap ai๐‘Ž๐‘– and bi๐‘๐‘– for any i๐‘– (1โ‰คiโ‰คn1โ‰ค๐‘–โ‰ค๐‘›). Each swap costs you ci๐‘๐‘– coins.Find the maximum possible value ofgcd(a1,a2,โ€ฆ,an)+gcd(b1,b2,โ€ฆ,bn)gcd(๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›)+gcd(๐‘1,๐‘2,โ€ฆ,๐‘๐‘›)that you can get by paying in total at most d๐‘‘ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the q๐‘ž possible values d1,d2,โ€ฆ,dq๐‘‘1,๐‘‘2,โ€ฆ,๐‘‘๐‘ž.InputThere are two integers on the first lineย โ€” the numbers n๐‘› and q๐‘ž (1โ‰คnโ‰ค5โ‹…1051โ‰ค๐‘›โ‰ค5โ‹…105, 1โ‰คqโ‰ค5โ‹…1051โ‰ค๐‘žโ‰ค5โ‹…105).On the second line, there are n๐‘› integersย โ€” the numbers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1081โ‰ค๐‘Ž๐‘–โ‰ค108).On the third line, there are n๐‘› integersย โ€” the numbers b1,b2,โ€ฆ,bn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰คbiโ‰ค1081โ‰ค๐‘๐‘–โ‰ค108).On the fourth line, there are n๐‘› integersย โ€” the numbers c1,c2,โ€ฆ,cn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰คciโ‰ค1091โ‰ค๐‘๐‘–โ‰ค109).On the fifth line, there are q๐‘ž integersย โ€” the numbers d1,d2,โ€ฆ,dq๐‘‘1,๐‘‘2,โ€ฆ,๐‘‘๐‘ž (0โ‰คdiโ‰ค10150โ‰ค๐‘‘๐‘–โ‰ค1015).OutputPrint q๐‘ž integersย โ€” the maximum value you can get for each of the q๐‘ž possible values d๐‘‘.ExamplesinputCopy3 41 2 34 5 61 1 10 1 2 3outputCopy2 3 3 3 inputCopy5 53 4 6 8 48 3 4 9 310 20 30 40 505 55 13 1000 113outputCopy2 7 3 7 7 inputCopy1 13450outputCopy7

...expand
๐Ÿง Not the exact question you are looking for?Go ask a question

Solution

This problem is a competitive programming problem and it's about finding the maximum possible value of the sum of the greatest common divisors (GCD) of two arrays of integers, given that you can

Similar Questions

You are given an array x2,x3,โ€ฆ,xn๐‘ฅ2,๐‘ฅ3,โ€ฆ,๐‘ฅ๐‘›. Your task is to find any array a1,โ€ฆ,an๐‘Ž1,โ€ฆ,๐‘Ž๐‘›, where:1โ‰คaiโ‰ค1091โ‰ค๐‘Ž๐‘–โ‰ค109 for all 1โ‰คiโ‰คn1โ‰ค๐‘–โ‰ค๐‘›.xi=aimodaiโˆ’1๐‘ฅ๐‘–=๐‘Ž๐‘–mod๐‘Ž๐‘–โˆ’1 for all 2โ‰คiโ‰คn2โ‰ค๐‘–โ‰ค๐‘›.Here cmodd๐‘mod๐‘‘ denotes the remainder of the division of the integer c๐‘ by the integer d๐‘‘. For example 5mod2=15mod2=1, 72mod3=072mod3=0, 143mod14=3143mod14=3.Note that if there is more than one a๐‘Ž which satisfies the statement, you are allowed to find any.InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค104)(1โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases.The first line of each test case contains a single integer n๐‘› (2โ‰คnโ‰ค500)(2โ‰ค๐‘›โ‰ค500)ย โ€” the number of elements in a๐‘Ž.The second line of each test case contains nโˆ’1๐‘›โˆ’1 integers x2,โ€ฆ,xn๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘› (1โ‰คxiโ‰ค500)(1โ‰ค๐‘ฅ๐‘–โ‰ค500)ย โ€” the elements of x๐‘ฅ.It is guaranteed that the sum of values n๐‘› over all test cases does not exceed 2โ‹…1052โ‹…105.OutputFor each test case output any a1,โ€ฆ,an๐‘Ž1,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1091โ‰ค๐‘Ž๐‘–โ‰ค109) which satisfies the statement.ExampleinputCopy542 4 131 164 2 5 1 2250031 5outputCopy3 5 4 92 5 115 14 16 5 11 24501 5002 7 5NoteIn the first test case a=[3,5,4,9]๐‘Ž=[3,5,4,9] satisfies the conditions, because:a2moda1=5mod3=2=x2๐‘Ž2mod๐‘Ž1=5mod3=2=๐‘ฅ2;a3moda2=4mod5=4=x3๐‘Ž3mod๐‘Ž2=4mod5=4=๐‘ฅ3;a4moda3=9mod4=1=x4๐‘Ž4mod๐‘Ž3=9mod4=1=๐‘ฅ4;

Let's consider all integers in the range from 11 to n๐‘› (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of gcd(a,b)gcd(๐‘Ž,๐‘), where 1โ‰คa<bโ‰คn1โ‰ค๐‘Ž<๐‘โ‰ค๐‘›.The greatest common divisor, gcd(a,b)gcd(๐‘Ž,๐‘), of two positive integers a๐‘Ž and b๐‘ is the biggest integer that is a divisor of both a๐‘Ž and b๐‘.InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1001โ‰ค๐‘กโ‰ค100) ย โ€” the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer n๐‘› (2โ‰คnโ‰ค1062โ‰ค๐‘›โ‰ค106).OutputFor each test case, output the maximum value of gcd(a,b)gcd(๐‘Ž,๐‘) among all 1โ‰คa<bโ‰คn1โ‰ค๐‘Ž<๐‘โ‰ค๐‘›.

You are given an integer x๐‘ฅ. Your task is to find any integer y๐‘ฆ (1โ‰คy<x)(1โ‰ค๐‘ฆ<๐‘ฅ) such that gcd(x,y)+ygcd(๐‘ฅ,๐‘ฆ)+๐‘ฆ is maximum possible.Note that if there is more than one y๐‘ฆ which satisfies the statement, you are allowed to find any.gcd(a,b)gcd(๐‘Ž,๐‘) is the Greatest Common Divisor of a๐‘Ž and b๐‘. For example, gcd(6,4)=2gcd(6,4)=2.InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค10001โ‰ค๐‘กโ‰ค1000)ย โ€” the number of test cases.Each of the following t๐‘ก lines contains a single integer x๐‘ฅ (2โ‰คxโ‰ค10002โ‰ค๐‘ฅโ‰ค1000).OutputFor each test case, output any y๐‘ฆ (1โ‰คy<x1โ‰ค๐‘ฆ<๐‘ฅ), which satisfies the statement.ExampleinputCopy710721100210006outputCopy56189817503

You are given a 0-indexed integer array nums, and you are allowed to traverse between its indices. You can traverse between index i and index j, i != j, if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor.Your task is to determine if for every pair of indices i and j in nums, where i < j, there exists a sequence of traversals that can take us from i to j.Return true if it is possible to traverse between all such pairs of indices, or false otherwise.ย Example 1:Input: nums = [2,3,6]Output: trueExplanation: In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1.To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.Example 2:Input: nums = [3,9,5]Output: falseExplanation: No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.Example 3:Input: nums = [4,3,12,8]Output: trueExplanation: There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.

Develop a Java program to find the GCD (Greatest Common Divisor) of two numbers.Input FormatTwo integers a and b separated by a space.ConstraintsThe absolute values of a and b will be less than or equal to 10^9. a and b are non-negative integers.Output FormatA single integer representing the GCD of a and b.Sample Input 012 18Sample Output 06Contest ends in an hourSubmissions: 0Max Score: 5Difficulty: EasyRate This Challenge: More Java 151import java.io.*;2import java.util.*;3โ€‹4public class Solution {5โ€‹6 ย  ย public static void main(String[] args) {7 ย  ย  ย  ย /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8 ย  }9}

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.