12

Doubt #1: Beautiful Tree

 2 years ago
source link: http://codeforces.com/blog/entry/83830
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

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)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK