HighC's blog

By HighC, history, 4 years ago, In English

Hello everyone. I encountered this question in an online test. I could not solve it. Any suggestion for solution will be highly appreciated.

Problem statement

You are given a Tree with n nodes rooted at 1. You are given a value m. You can assign the nodes any value between 1 to m (inclusive). You have to count number of ways to make a Beautiful Tree. A Tree is called a Beautiful Tree, if and only if there exist at-least one root to leaf path, such that GCD of the the values of corresponding nodes in the Path is greater than 1. Since answer can be very large, output answer modulo 10^9 + 7.

Input format:

First line contains two integers n, m. Each of next n-1 lines contains two integers a and b, denoting edge between a and b.

constraints

1 <= n <= 10^5 m < 20

Sample Input:

3 2
1 2
1 3

Sample Output:

3

Explanation:

node 1 (value 2) -> node 2 (value 2) -> node 3 (value 2)
node 1 (value 2) -> node 2 (value 2) -> node 3 (value 1)
node 1 (value 2) -> node 2 (value 1) -> node 3 (value 2)

Full text and comments »

  • Vote: I like it
  • -12
  • Vote: I do not like it