tinycodinglm / cf /cf-1010-C.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
3.83 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

User

Problem: Border (Rating: 1800)
Tags: number theory

Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.

There are $n$ banknote denominations on Mars: the value of $i$-th banknote is $a_i$. Natasha has an infinite number of banknotes of each denomination.

Martians have $k$ fingers on their hands, so they use a number system with base $k$. In addition, the Martians consider the digit $d$ (in the number system with base $k$) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base $k$ is $d$, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.

Determine for which values $d$ Natasha can make the Martians happy.

Natasha can use only her banknotes. Martians don't give her change.

Input: The first line contains two integers $n$ and $k$ ($1 \le n \le 100,000$, $2 \le k \le 100,000$) — the number of denominations of banknotes and the base of the number system on Mars.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — denominations of banknotes on Mars.

All numbers are given in decimal notation.

Output: On the first line output the number of values $d$ for which Natasha can make the Martians happy.

In the second line, output all these values in increasing order.

Print all numbers in decimal notation.

Examples:
Input: 2 8
12 20
Output: 2
0 4

Input: 3 10
10 20 30
Output: 1
0

Assistant

Note that the condition "the last digit in the record of Natasha's tax amount in the number system with the base $$$k$$$ will be $$$d$$$" is equivalent to the condition "the remainder of dividing the tax on $$$k$$$ will be $$$d$$$".
Let $$$g=GCD(a_1,a_2,\ldots,a_n)$$$. It is stated that the original problem is equivalent to the problem where $$$n=1$$$ and the only banknote is $$$g$$$.
Evidence. We prove this with the help of the Bézout's identity. It follows that an equation of the form $$$a_1$$$$$$x_1+a_2$$$$$$x_2+\dots+a_n$$$$$$x_n=c$$$, where at least one of the parameters $$$a_1,a_2,\dots,a_n$$$ is not zero, has a solution in integers if and only if $$$c \hspace{2pt} \vdots \hspace{2pt} GCD(a_1,a_2,…,a_n)$$$. Then in this task Natasha can pay $$$x_i$$$ banknotes of the $$$i$$$-th nominal value for each $$$i$$$, where $$$1\le i\le n$$$, and the amount of tax ($$$a_1$$$$$$x_1+a_2$$$$$$x_2+\dots+a_n$$$$$$x_n$$$) can be any number $$$c$$$, multiple $$$g$$$. (Here some $$$x_i<0$$$, But Natasha can add for each par a sufficiently large number, multiple $$$k$$$, that $$$x_i$$$ became greater than zero, the balance from dividing the amount of tax on $$$k$$$ from this will not change.) Therefore, you can replace all pars with one par $$$g$$$ and the answer from this will not change.
Now we can sort out all the numbers of the form $$$gx\bmod k$$$, where $$$0\le x<k$$$ (further the remainder of the sum, divided by $$$k$$$ will cycle repeatedly) and output them in ascending order.
Complexity: $$$O(n+\log m+k\log k)$$$, where $$$m$$$ is the greatest $$$a_i$$$.
Bonus. Try to improve the complexity to $$$O(n+k)$$$.