[BOJ 7651] G-Avoiding Sequence

View as PDF

Submit solution

Points: 5
Time limit: 1.0s
Memory limit: 128M

Problem types
Allowed languages
Assembly, Awk, C, C++, Java, Pascal, Perl, Python, Sed, Text

Given a set of distinct integers S and an integer G, we call a sequence of integers a G-Avoiding Sequence if the following two conditions hold:</p>

  1. the sequence is a permutation of the elements of S; and
  2. for any two consecutive elements A and B in the sequence, A − B is not divisible by G.

Compute the number of G-Avoiding Sequences modulo the prime 1,234,567,891.

입력 형식

The input consists of several test cases. The first line of each test case contains two integers N (1 ≤ N ≤ 200), the size of S, and G (1 ≤ G ≤ 1,000). The next line will contain N integers, which are the elements of S and between 0 and 106. Input is followed by a single line with N = G = 0, which should not be processed.

출력 형식

For each test case, output a single line containing the number of G-Avoiding Sequences modulo the prime 1,234,567,891.

예제 입력

4 3
1 2 3 4
3 100
10 110 42
0 0

예제 출력

12
2

Comments

There are no comments at the moment.