PROBLEM LINK:Practice Setter: Evgeniy Artemov DIFFICULTY:EasyMedium PREREQUISITES:Sieve of Eratosthenes, NumberTheory and Constructive Algorithms. PROBLEM:Given an integer $N \geq 3$, find $N$ distinct numbers such that every pair of consecutive numbers have a common factor $ > 1$ and all possible consecutive three elements have no common factor $ > 1$. Also, all numbers found should be between $1$ and $10^9$. First and Last elements are also considered adjacent here. SUPER QUICK EXPLANATION
EXPLANATIONThis is a constructive problem, and hence, have multiple solutions. Feel free to share your solution in comments. Note: If we represent a number as the product of prime numbers, Prime factorization of a common factor of the pair of numbers is the common part of prime factorization. If we consider prime values or product of few primes, they have relatively lesser factors as compared to composite numbers. So, for most of the problems involving prime factors/factorization/divisibility, prime numbers bear special importance. Let us try similar problems first. Try solving the same problem assuming there's no upper bound on maximum values. Now, we can just multiply a distinct prime number to every pair of consecutive numbers. This way, every two consecutive numbers are not coprime while every three consecutive numbers are coprime. The above solution is sufficient to solve the first subtask, but in the second subtask, the values exceed $10^9$, so we need something better. Try solving the same problem, assuming we are allowed repeated values. Here, we can choose prime numbers and multiplying each position by exactly two primes such that every two consecutive positions share prime factor while every three consecutive numbers do not share any prime factor, taking special care to choose a different prime number when reaching the end of the array. Now, let us solve the original problem. Think up and try to mix the above solutions so as to reduce the magnitude of generated numbers while keeping each number distinct. The construction used in my solution is to reserve the first few primes and then multiply the first two positions by a nonreserved prime, next two positions by another prime and so on. Now, We can multiply Last and first position by a reserved prime, second and third position by different reserved prime, and so on. This way, we are guaranteed to have every pair of consecutive positions divisible by that particular prime number, and every three consecutive positions are coprime since no prime is multiplied to three consecutive positions. Implementation For this problem, we can preprocess and calculate a list of primes beforehand using the sieve of Eratosthenes and then for every test case, use primes from the list instead of recomputing. Interesting Fact This problem can also be approached as a challenge problem where we need to minimize the maximum value generated in sequence. Can you minimize? Say for $N = 50000$ Time ComplexityTime complexity is $O(MX*log(log(MX)) + \sum N)$ per test case where $MX$ is the upper limit till primes are found. Finding primes up to $10^6$ suffices for this problem. AUTHOR'S AND TESTER'S SOLUTIONS:Setter's solution Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)
This question is marked "community wiki".
asked 14 Jan, 16:08

I did by finding Euler Circuit of a complete graph of odd degree, and then adjusting it by removing the last edge and adding path of required length. answered 14 Jan, 20:33
Here's my solution : https://www.codechef.com/viewsolution/22464087
(14 Jan, 22:51)

What I did was... 1) $n \mod 3 == 0$6, 10, 15, 6*p1, 10*p2, 15*p3, 6*p4, 10*p5, ... so on2) $n \mod 3 == 1$21, 14, 10, 15, 6*p1, 10*p2, 15*p3, 6*p4, 10*p5, ... so on3) $n \mod 3 == 2$33, 77, 14, 10, 15, 6*p1, 10*p2, 15*p3, 6*p4, 10*p5, ... so onWhere p1,p2,p3,... are primes greater than 11...(as I used primes till 11 in these case to make sequence N%3==0) answered 14 Jan, 22:06
basic idea is keep repeating 6,10,15 keeping all number different by multiplying with different primes...
(14 Jan, 22:10)
HERE is my solution
(14 Jan, 22:24)

One interesting property which I found was that "No three consecutive odd numbers have a common divisor" starting from 3. So, I tried multiplying 3 to the first 2 elements, then multiplied 5 to the second and third elements, then multiplied 7 to the third and fourth elements and so on... (completing the cycle with a prime number for the last and the first elements just in case if the first element and the second last element don't have a common divisor). This passed subtask1, but didn't pass subtask2. Maybe it was because of generating numbers > 10^9. Is there a modification I could do to this approach to get it ACed? answered 16 Jan, 11:01

I am a bit disappointed that the property of coprimes weren't considered in the editorial. In fact, it is not necessary at all to generate prime numbers. If we consider each positive integer as a multiset of its prime factors, then the problem could be reduced to: Find N ordered multisets of integers that for every 2 consecutive sets, the intersection is not empty, but the each intersection of 2 consecutive intersections is an empty set, i.e. coprime to each other. Therefore if we find a list of consecutive coprimes, the result would be product of 2 consecutive elements of that list. Implementation in Python:
Since the first 2 coprimes are hardcoded to be primes, we can reuse the list for every N in the given constraint, thus the overall complexity is drastically reduced. answered 15 Jan, 09:23

What i did was ... 1)found out out some 4000 consecutive prime numbers using seive 2)appended them to a new array under the condition that the product of two consecutive numbers does not cross 10^9.This helped me pass the first sub task. 3)For the next part I started my series from 5 and accepted primes at different intervals starting from 2 and incrementing interval by 1 and also a different starting point for every interval. 4)this helped me generate all(50000 in this case) possible combinations of products of primes and all i had to do was consider the first 550 prime numbers, and also including the previous The time complexity was O(d^3),d=550. but since the inner loops depended on the interval it worked even faster. answered 15 Jan, 16:03

my solution does not use generating primes at all. we forget some facts about numbers with small difference and their common factors.Here is my solution answered 16 Jan, 16:56

in java only 12500 primes can be stored for 50000 Bytes answered 17 Jan, 21:47

https://www.codechef.com/viewsolution/22395844 what is wrong in this solution?. answered 18 Jan, 10:12
