#P3962. [TJOI2013] 数字根

[TJOI2013] 数字根

Description

The digital root of a number is defined as repeatedly summing its digits until the result is less than 10. For example, the digital root of 64357 is 7, because 6+4+3+5+7=256 + 4 + 3 + 5 + 7 = 25, and 2+5=72 + 5 = 7. The digital root of an interval is defined as the digital root of the sum of all numbers in that interval.

Given a sequence A1,A2,A3,,AnA_1, A_2, A_3, \ldots, A_n, you need to answer several queries. For each query, you are given an interval [l,r][l, r]. Among all contiguous subintervals within [l,r][l, r], find the top 5 distinct digital roots. If there are fewer than 5, fill the remaining positions with 1-1.

Input Format

  • The first line contains an integer NN, the length of the sequence.
  • The second line contains NN integers AiA_i (0Ai<1090 \le A_i < 10^9).
  • The third line contains an integer QQ, the number of queries.
  • Each of the next QQ lines contains two integers l,rl, r (1lrN1 \le l \le r \le N), denoting a query interval.

Output Format

Output QQ lines. For each query interval, output the top 5 distinct digital roots of all its contiguous subintervals in descending order, space-separated. If there are fewer than 5, pad with 1-1.

5
101 240 331 4 52
3
1 3
4 5
1 5
8 7 6 4 2
7 4 2 -1 -1
9 8 7 6 4

Hint

Sample Explanation

For the first query interval [1,3][1, 3], its contiguous subintervals are [1,1][1, 1], [2,2][2, 2], [3,3][3, 3], [1,2][1, 2], [2,3][2, 3], [1,3][1, 3]. Their corresponding digital roots are 2,6,7,8,4,62, 6, 7, 8, 4, 6. Therefore, the top 5 are 8,7,6,4,28, 7, 6, 4, 2.

Constraints

  • 30% of the testdata: N1000N \le 1000, Q1000Q \le 1000.
  • 100% of the testdata: N100000N \le 100000, Q100000Q \le 100000.

Translated by ChatGPT 5