Connection Status:
Competition Arena > ColorfulLineGraphsDiv2
SRM 661 · 2015-05-01 · by lg5293 · Advanced Math, Math, Simple Math
Class Name: ColorfulLineGraphsDiv2
Return Type: int
Method Name: countWays
Arg Types: (int, int)
Problem Statement

Problem Statement

Bob is going to create a graph with N nodes. The graph will be constructed in two steps. First, Bob will take N isolated vertices, label them 1 through N and color each of them using one of K colors. Then, Bob will add some directed edges to the graph. For each i between 2 and N, inclusive, Bob may choose a single value j < i such that the nodes i and j have different colors. If he does, he will add the edge from i to j to his graph. Note that Bob may choose not to add any edge from node i, even if there are some valid choices of j.

Two graphs are considered the same if they have the same node colors and the same set of edges.

You are given the ints N and K. Compute and return the number of different graphs Bob may construct, modulo 1,000,000,007.

Constraints

  • N will be between 1 and 100, inclusive.
  • K will be between 1 and 3, inclusive.
Examples
0)
3
2
Returns: 24

The 24 different graphs are shown below. In each picture, the vertices have labels 1, 2, 3 from the left to the right.

1)
15
2
Returns: 789741546
2)
100
1
Returns: 1
3)
1
3
Returns: 3
4)
100
3
Returns: 492594064

Submissions are judged against all 45 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ColorfulLineGraphsDiv2 with a public method int countWays(int N, int K) · 45 test cases · 2 s / 256 MB per case

Submitting as anonymous