Problem · Tree
Prime Tree City Labelings
Learn this problemProblem statement
Hackerland contains n cities numbered from 1 to n. Its n - 1 roads form a tree, where road i connects edgeFrom[i] and edgeTo[i].
Assign one prime number from 2 through 100, inclusive, to every city. Prime values may be reused. For every road, the sum of the prime values assigned to its two endpoints must not be prime.
Return the number of valid assignments modulo 10^9 + 7.
Function
countPrimeLabelings(n: int, edgeFrom: int[], edgeTo: int[]) → intExamples
Example 1
n = 2edgeFrom = [1]edgeTo = [2]return = 609Among all ordered assignments of primes from 2 through 100 to the two cities, 609 have a non-prime endpoint sum.
Example 2
n = 1edgeFrom = []edgeTo = []return = 25There are 25 primes from 2 through 100, and a one-city tree has no road constraint.
Constraints
1 <= n <= 200000edgeFrom.length = edgeTo.length = n - 1- The roads form a tree.
More Rippling problems
- Delivery Cost TrackerPHONE SCREEN · Seen Jul 2026
- Corporate Card Expense RulesPHONE SCREEN · Seen Jun 2026
- Camel CardsPHONE SCREEN · Seen May 2026
- Article Vote TrackerPHONE SCREEN · Seen May 2026
- Employee Resource Access ManagementONSITE INTERVIEW · Seen Jan 2026
- Limit an Organization Tree's HeightONSITE INTERVIEW · Seen Aug 2025
- Distributed System RecoveryOA · Seen Jul 2025
- Server Upgrade PlanningOA · Seen Jul 2025