CodeChef: May Long Challenge | Tree House | THOUSES | Solution

Codechef

View on Codechef

There is a large tree house in an unknown world. It is ruled by the great emperor KZS. It consists of NN nodes numbered from 11 to NN in which the people of that world reside. The nodes are organized in a tree structure rooted at node 11. You need to assign values to the nodes according to the wishes of emperor KZS which are as follows :-

  • The value of node 11 is XX.
  • All immediate children of any node have pairwise distinct values.
  • For every node with at least one immediate child, the gcdgcd of the values of all immediate children is equal to the value of the node.
  • The total sum of the values of all nodes should be minimum.

The greatest common divisor gcd(a,b)gcd(a,b) of two positive integers aa and bb is equal to the largest integer dd such that both integers aa and bb are divisible by dd.

Print the sum of all values, modulo 109+7109+7.

Input

  • The first line contains an integer TT, the number of test cases. TT testcases follow.
  • The first line of each test contains two integers NN and XX.
  • Each of the following N−1N−1 lines contains two integers uu and vv, denoting an edge between nodes uu and vv.

Output

  • For each test case, print the sum of values, modulo 109+7109+7.

Constraints

  • 1≤T≤151≤T≤15
  • 2≤N≤3⋅1052≤N≤3⋅105
  • 1≤X≤1091≤X≤109
  • 1≤u,v≤N1≤u,v≤N and u≠vu≠v
  • The given edges form a tree
  • The sum of NN over all test cases doesn’t exceed 3⋅1053⋅105.

Subtasks

Subtask #1 (100 points): Original Constraints

Sample Input

2
4 1
1 2
1 3
1 4
8 1
1 2
1 3
2 4
2 5
5 6
5 7
7 8

Sample Output

7
11

Explanation

In test case 11, we will give values 11, 22, 33 to the nodes 22, 33 and 44 respectively. So, the total sum will be 1+1+2+3=71+1+2+3=7.

Solution will be updated here soon. Keep tracking

See more posts here

Leave a Comment

Your email address will not be published. Required fields are marked *

x