#P17399. [ICPC 2018 Shenyang R] Rainbow Graph
[ICPC 2018 Shenyang R] Rainbow Graph
Problem Description
A graph without loops or multiple edges is known as a simple graph.
A vertex-colouring is an assignment of colours to each vertex of a graph. A proper vertex-colouring is a vertex-colouring in which no edge connects two identically coloured vertices.
A vertex-colouring with colours of an undirected simple graph is called an -rainbow colouring if every colour appears once, and only once, on all the adjacent vertices of each vertex. Note that an -rainbow colouring is not a proper colouring, since adjacent vertices may share the same colour.
An undirected simple graph is called an -rainbow graph if the graph can admit at least one legal -rainbow colouring. Two -rainbow graphs and are called isomorphic if, between the sets of vertices in and , a bijective mapping exists such that two vertices in are adjacent if and only if their images in are adjacent.
Your task in this problem is to count the number of distinct non-isomorphic -rainbow graphs having vertices and report that number modulo a prime number .
Input Format
The input contains several test cases, and the first line contains a positive integer indicating the number of test cases which is up to .
For each test case, the only line contains two integers and where , and is a prime.
We guarantee that the numbers of test cases satisfying , and are no larger than , and respectively.
Output Format
For each test case, output a line containing "Case #x: y" (without quotes), where is the test case number starting from , and is the answer modulo .
5
1 11059
2 729557
3 1461283
4 5299739
63 49121057
Case #1: 1
Case #2: 1
Case #3: 2
Case #4: 3
Case #5: 5694570
Hint
If you came up with a solution such that the time complexity is asymptotic to , the number of partitions of , or similar, you might want to know , , and .
The following figures illustrate all the non-isomorphic rainbow graphs mentioned in the first four sample cases.
:::align{center}

Figure 1: the non-isomorphic 1-rainbow graph with 2 vertices

Figure 2: the non-isomorphic 2-rainbow graph with 4 vertices

Figure 3: the non-isomorphic 3-rainbow graphs with 6 vertices

Figure 4: the non-isomorphic 4-rainbow graphs with 8 vertices :::