Factor Trees
Practice
5 (3 votes)
Dynamic programming
Recursion
Medium
Mathematics
Sieve
Approved
Factorization
Problem
71% Success 886 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A factor tree is a binary tree where each non-leaf node p has two children q and r, such that \(p = q * r\) and \(r \ne 1, q \ne 1\). Prime factors are treated as leaf nodes of the tree.
Following are the factor trees of \(24\) :
enter image description here

These are the four possible factor trees of \(24\).
Since Fredo loves doing experiment, he wants to know how many different factor trees are there whose root lies in the range \([l,r]\) and the tree contains x as a node.

Two factor trees are different if and only if they are not isomorphic.

Two trees are called isomorphic if one of them can be obtained from other by a series of flips, i.e. by swapping left and right children of a number of nodes. Any number of nodes at any level can have their children swapped. Two empty trees are isomorphic.

enter image description here

These two trees are isomorphic.

The above four factor trees of \(24\) are different as they hold the above definition.

Fredo has written a program to know the different factor trees as asked in the question but he asks you to write a program to check whether he did it right or not.

Input:
First line consists of an integer T, representing the number of test cases.
Each of the following T lines consists of three integers \(x,l,r\) as described in the question.

Output:
Print the number of different factor trees for each query in a separate line.

Constraints:

  • \(1 \le T \le 10^5\)
  • \(2 \le x \le 5 * 10^2\)
  • \(2 \le l \le r \le 5 * 10^3\)

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
5 votes
Tags:
MathematicsDynamic programmingMediumNumber TheoryFactorization
Points:30
9 votes
Tags:
MediumAlgorithms
Points:30
1 votes
Tags:
mathsprime factorizationsieveMedium