Doubt #1: Beautiful Tree

Revision en2, by HighC, 2020-10-19 06:45:08

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)
Tags tree

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English HighC 2020-10-19 06:45:08 51
en1 English HighC 2020-10-19 05:51:42 1148 Initial revision (published)