Micro and Lucky Tree

4.9

14 votes
Algorithms, Approved, Bit Manipulation, Dynamic Programming, Medium
Problem

Micro purchased a tree having N nodes numbered from 1 to N. It is rooted at node numbered 1. But unfortunately that tree turned out to be bad luck. After he purchased that tree, he lost his job and girlfriend. So he went to his astrologer friend Mike for help.

Mike told him to assign a value in the range 1 to M (inclusive) to each node making sure that luck factor of all leaf nodes is 1. Luck factor of a leaf node v is defined as gcd of values of all nodes lying in path from root to v (inclusive). Now Micro wants to know how many ways are there to make his tree lucky. That's where Mike failed, so he asked for your help.

Input:
First line consists of a single integer denoting N and M.
Each of the following N1 lines consists of two space separated integers X and Y denoting there is an edge between nodes numbered X and Y.

Output:
Print the number of ways to make the tree lucky. Since the answer can be large, print it modulo 109+7.

Constraints:
1N105
1M20
1X,YN

Sample Input
3 2
1 2
1 3
Sample Output
5
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Following are the 5 valid ways:
val[1]=2, val[2]=1, val[3]=1
val[1]=1, val[2]=1, val[3]=2
val[1]=1, val[2]=2, val[3]=2
val[1]=1, val[2]=1, val[3]=1
val[1]=1, val[2]=2, val[3]=1

Editor Image

?