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


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.


  • 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.


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


  • 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.


Subtask #1 (100 points): Original Constraints

Sample Input

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



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

9 thoughts on “CodeChef: May Long Challenge | Tree House | THOUSES | Solution”

  1. I think this iѕ one of the most significant info for me.
    And i am glad reading уoսr article. But should remarқ on few general things, The site style is ideal,
    the articles is really great : D. Good job, cheers

  2. What’s up, thіs weekend is pleɑsant designed for me, because
    thіs point in time i am reading this great educational paragraph
    here at my residence.

  3. Thanks , I have recently been searching for info approximately this topic
    for a while and yours is the best I have found out till now.
    However, what in regards to the conclusion? Are you positive about the source?

  4. Wow, wonderful blog layout! How long have you been blogging for?

    you make blogging look easy. The overall look of your web site is wonderful, let alone the content!

  5. Good day! This is my first comment here so I
    just wanted to give a quick shout out and tell you I truly enjoy reading your articles.
    Can you suggest any other blogs/websites/forums that go over the same
    topics? Thanks!

  6. You can certainly see your skills within the work you write.
    The world hopes for even more passionate writers such as you who aren’t afraid to say how they believe.
    At all times follow your heart.

Leave a Comment

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