Count Number of Sequences CodeChef Solution – Code Drive Solution

About Count Number of Sequences CodeChef Solution

  • This is a coding contest based on algorithms, data structures, and problem-solving.
  • Organizer: The contest is hosted by NIT Trichy.
  • Prizes: NA
  • Registrations for prizes: NA

Problem: Count Number of Sequences CodeChef Solution

Define f(x)f(x) to be the largest divisor of xx which is not equal to xx; in the case of x=1x=1, we define f(1)=1f(1)=1 as an exception.

You are given a tree containing NN nodes numbered from 11 to NN, where each node ii has a value ViVi assigned to it. Define g(X,R)g(X,R) with 1≤X,R≤N1≤X,R≤N as the number of arrays of nodes AA that satisfy the following conditions:

  • 1≤Ai≤N1≤Ai≤N (i.e. AiAi represents a node on the tree).
  • A1=XA1=X.
  • Ai≠AjAi≠Aj for all 1≤i<j≤|A|1≤i<j≤|A|.
  • The distance between node AiAi and A1A1 is no more than RR for all 1<i≤|A|1<i≤|A|. The distance between two nodes AiAi and A1A1 on a tree is defined as the number of edges on the simple path from AiAi to A1A1.
  • VAi=f(VAi−1)VAi=f(VAi−1) or VAi=VAi−1VAi=VAi−1 for all 1<i≤|A|1<i≤|A|.

Two arrays are considered the same if both have the same length and same element at each position.

You are given QQ queries, each of which contains two integers XX and RR, and for each of which you need to find the value g(X,R)g(X,R). As this number can be very large, output it under modulo 109+7109+7.

Input Format

  • The first line of the input contains an integer NN – the number of nodes in the tree.
  • The next line contains NN space-separated integers V1,V2,…,VNV1,V2,…,VN – the values of the nodes.
  • Each of N−1N−1 lines contains two space-separated integers uu and vv denoting an edge between node uu and vv on the tree.
  • The next line contains an integer QQ – the number of queries.
  • Each of the next QQ lines contains two space-separated integer XX and RR – the initial node and the specified distance of that query.

Output Format

For each query, output on a single line g(X,R)mod109+7g(X,R)mod109+7.


  • 1≤N≤1051≤N≤105
  • 1≤Vi≤1051≤Vi≤105
  • 1≤u,v≤N1≤u,v≤N
  • u≠vu≠v
  • 1≤Q≤1041≤Q≤104
  • 1≤X,R≤N1≤X,R≤N

Sample Input 1 

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

Sample Output 1 



  • Query 11: The only possible array is [3][3].
  • Query 22: The possible arrays are:
    • [3][3]
    • [3,1][3,1]
    • [3,1,4][3,1,4]
    • [3,1,7][3,1,7]
    • [3,1,4,7][3,1,4,7]
    • [3,1,7,4]

Solution: Count Number of Sequences CodeChef Solution

All you need to do is to compute row N of the triangle (code can be found here) and then sum the appropriate entries:

row = mahonian_row(N)
print sum(row[n] for n in range(K+1) if n%2==K%2 and n<len(row))

Contest Details:

  • This is an External Rated Contest.
  • Duration: 3 Hours
  • Start time: 30th December 2021, 20:00 hrs IST
  • End time: 30th December 2021, 23:00 hrs IST
    You may check your timezone here.
  • This contest is rated only for Division 2 and Division 3 users. Division 1 users can participate unofficially in this contest.

Get More CodeChef Solution >>

Substring Minimum Function FizzBuzz Solution

Magical Planks Fizzbuzz Solution

Favourite String of Chef CodeChef Solution

Sum this up Code Chef Solution

Game between friends CodeChef Solution

Leave a Reply

Your email address will not be published.

error: Content is protected !!