Digital Hallucinations
YOU: "The banners are lovely. You were in charge of those?"
MARCUS: "Sure was. It's nice when someone actually appreciates the real, human effort that goes into these things. Nobody does anymore."
YOU: "If the banners were already made, why were you here helping set up?"
MARCUS: "Kevin asked me to help Ritisha put them up. Found her the best spots, held the step-ladder steady. Safety first."
YOU: "Did you talk to anyone else that afternoon?"
MARCUS: "Nah, just Ritisha, the entire time. She's great company, didn't feel like I needed to talk to anyone else. Think she needed it, too. She was down about having her responsibility taken off her. That's a bit typical for this club."
Marcus has been working on ACPC Ball banners for twelve hours straight, and he is beginning to go a bit loopy. He has an array of numbers
, where
is the number of copies of banner
that Marcus needs to make. Sadly, on such little sleep, Marcus begins to feel gripped by many emotions.
Marcus Feels Happy
First, each value is replaced by its happiness value
. The happiness value of a positive integer
is the number of integers that have between
and
digits (inclusive), where no digit appears a strict majority number of times.
More specifically, a number with digits qualifies if and only if every digit value appears at most
times. Numbers with leading zeros do not qualify.
Each happiness value is taken modulo .
For instance, the happiness values for
are
.
Marcus Feels Sad
Next, each value is split into its digits. A number with digits becomes
single-digit elements in the array.
For instance,
contributes the four elements
to the array. A value with fewer digits contributes fewer elements;
contributes the single element
.
Marcus feels Angry
Finally, the digits skitter around. Marcus counts the number of distinct permutations of the array with at most inversions. For some array
, an inversion is a pair of positions
where
. Permutations that differ only by swapping equal digits are identical in this step.
Output this final count modulo .
For instance, the array
has
arrangements with
inversion,
and
.
Input
The first line contains two integers, and
(
), the length of Marcus' initial array and his inversion-counting limit.
The second line contains integers
(
), the values in Marcus' initial array.
Output
Output Marcus' final value after feeling happy, then sad, then angry, modulo .
Example
Input 1
4 2
1 2 1 3
Output 1
21
Marcus Feels Happy
- There are
numbers of length
with no digits appearing more than
times.
- There are
numbers of length
with no digits appearing more than
time.
- There are
numbers of length
with no digits appearing more than
times.
Therefore, when Marcus feels happy, his array becomes 0, 81, 0, 729.
Marcus Feels Sad
Splitting by digits, Marcus' array becomes 0, 8, 1, 0, 7, 2, 9.
Marcus Feels Angry
There are arrangements of
0, 8, 1, 0, 7, 2, 9 with at most inversions:
0, 0, 1, 2, 7, 8, 90, 0, 1, 2, 7, 9, 80, 0, 1, 2, 8, 7, 90, 0, 1, 7, 2, 8, 90, 0, 2, 1, 7, 8, 90, 1, 0, 2, 7, 8, 90, 0, 1, 2, 8, 9, 70, 0, 1, 2, 9, 7, 80, 0, 1, 7, 2, 9, 80, 0, 1, 7, 8, 2, 90, 0, 1, 8, 2, 7, 90, 0, 2, 1, 7, 9, 80, 0, 2, 1, 8, 7, 90, 0, 2, 7, 1, 8, 90, 0, 7, 1, 2, 8, 90, 1, 0, 2, 7, 9, 80, 1, 0, 2, 8, 7, 90, 1, 0, 7, 2, 8, 90, 1, 2, 0, 7, 8, 90, 2, 0, 1, 7, 8, 91, 0, 0, 2, 7, 8, 9
Input 2
2 1
10 10
Output 2
5
Marcus Feels Happy
- There are
numbers of length
with no digits appearing more than
times.
Therefore, when Marcus feels happy, his array becomes 128912009, 128912009.
Marcus Feels Sad
Splitting by digits, Marcus' array becomes 1, 2, 8, 9, 1, 2, 0, 0, 9, 1, 2, 8, 9, 1, 2, 0, 0, 9.
Marcus Feels Angry
There are arrangements of
1, 2, 8, 9, 1, 2, 0, 0, 9, 1, 2, 8, 9, 1, 2, 0, 0, 9 with at most inversion:
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 8, 8, 9, 9, 9, 90, 0, 0, 0, 1, 1, 1, 2, 1, 2, 2, 2, 8, 8, 9, 9, 9, 90, 0, 0, 0, 1, 1, 1, 1, 2, 1, 2, 2, 8, 8, 9, 9, 9, 90, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 8, 9, 8, 9, 9, 90, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 8, 8, 9, 9, 9, 9
Input 3
10 10
1 2 3 4 5 6 7 8 9 10
Output 3
1055872
Comments