Micro and Lucky Tree
Practice
4.9 (14 votes)
Algorithms
Approved
Bit manipulation
Dynamic programming
Medium
Problem
71% Success 1296 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(N-1\) 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 \(10^9+7\).

Constraints:
\(1 \le N \le 10^5\)
\(1 \le M \le 20\)
\(1 \le X, Y \le N\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
Dynamic ProgrammingDynamic Programming and Bit MaskingIntroduction to Dynamic Programming 1AlgorithmsBitmask1-D
Points:30
5 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
47 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMediumReady