G-Avoiding Sequence
Time limit1sMemory limit128 MB
Count permutations of a set where consecutive elements never differ by a multiple of G, modulo a prime.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Math
- Solved
- No attempts yet
Problem
You are given a set of distinct integers and an integer . A sequence of integers is called a G-Avoiding Sequence if both of the following conditions hold:
- the sequence is a permutation of the elements of ; and
- for any two consecutive elements and in the sequence, is not divisible by .
Compute the number of G-Avoiding Sequences modulo the prime 1,234,567,891.
Input
The input consists of several test cases. The first line of each test case contains two integers (), the size of , and (). The next line contains integers, the elements of , each between and .
The input ends with a single line containing , which must not be processed.
Output
For each test case, output a single line containing the number of G-Avoiding Sequences modulo 1,234,567,891.