#P44. [KBC003D] Flu 2

[KBC003D] Flu 2

Source

This problem is adapted from Long Long OJ. All rights reserved.

Problem Background

In the distant Gensokyo, an outbreak of idiocy flu has occurred.

Problem Description

There are MM residents living in Gensokyo, each with a unique and distinct personal ID number ranging from 00 to M1M-1.

On the first day, NN people are infected, and we know their IDs. In the subsequent days, a person with ID pp will be infected on a certain day if and only if:

  1. Some resident aa was infected on the previous day;
  2. Some resident bb was infected on the first day (note: aa and bb can be the same here);
  3. aa and bb satisfy the condition: a×bp(modM)a\times b≡p\pmod M.

Each resident can be infected repeatedly! Please write a program to find out which people will be infected on the KK-th day.

Input Format

  • The first line contains three integers K,M,NK, M, N;
  • The second line contains NN integers representing the IDs of people infected on the first day.

Output Format

Output a line with several integers, representing the IDs of people infected on the KK-th day (output in ascending order).

Samples

1 100 3
1 2 3
1 2 3
2 100 3
1 2 3
1 2 3 4 6 9
3 101 2
5 50
24 38 63 77

Data Range

For 100% of the data: 1K10181\le K\le 10^{18}, 3M15003\le M\le 1500, 0NM10 \le N \le M-1.