Greatest Common Divisor
InterviewTime limit2sMemory limit512 MB
Given n numbers, choose a subset whose greatest common divisor is exactly d, or report -1 if no such subset exists.
- Level
Easy3 of 10
- Topics
- Number theory, Implementation
- Solved
- No attempts yet
Statement
Petya wrote numbers on a sheet of paper. They are the house numbers where his friends live. He wants to choose a subset of these numbers whose greatest common divisor is equal to his favorite number . Help Petya choose the subset.
Input
The first line contains two integers and (, ). The second line contains integers ().
Output
If a suitable subset exists, print its size on the first line. Print the numbers in the subset on the second line. If no solution exists, print on the first line. If there are several answers, print any of them.