#P15066. [UOI 2024 II Stage] GCD, Sum, Multiply. What?...

[UOI 2024 II Stage] GCD, Sum, Multiply. What?...

题目描述

The author has used up all the creative skills on previous problems, so Anton won't be tortured in this statement. He will just give you an interesting problem.

You are given an array aa consisting of nn integers. You are also given qq queries [l;r][l;r]. For each query, find the maximum value of $\operatorname{sum}[tl;tr] \times \operatorname{gcd}[tl;tr]$ over all pairs (tl;trtl;tr), where

  • ltltrrl \le tl \le tr \le r;
  • sum[tl;tr]\operatorname{sum}[tl;tr] --- the sum of all numbers in the segment [tl;tr][tl;tr];
  • gcd[tl;tr]\operatorname{gcd}[tl;tr] --- the greatest common divisor of all numbers in the segment [tl;tr][tl;tr].

The greatest common divisor of two numbers aa and bb is the largest positive integer xx that divides both aa and bb.

The greatest common divisor of a set of numbers is the largest positive integer xx that divides all elements of the set.

输入格式

The first line contains two integers nn, qq (1n,q21051 \le n, q \le 2 \cdot 10^5) --- the number of elements in the array and the number of queries, respectively.

The second line contains nn integers aia_i (1ai61061 \le a_i \le 6 \cdot 10^6) --- the description of the array.

Each of the next qq lines contains two integers ll, rr (1lrn1 \le l \le r \le n) --- the description of the queries.

输出格式

Print qq integers --- the answers to the queries.

3 2
3 3 2
1 3
2 3
18
9
8 6
2 4 8 8 8 2 4 16
1 8
2 5
3 4
2 4
7 7
3 6
256
192
128
128
16
192

提示

In the first example, there are following segments:

  • [1;1][1;1] --- $\operatorname{sum}[1;1] \times \operatorname{gcd}[1;1]$ = 3×33 \times 3 = 99;
  • [1;2][1;2] --- $\operatorname{sum}[1;2] \times \operatorname{gcd}[1;2]$ = 6×36 \times 3 = 1818;
  • [1;3][1;3] --- $\operatorname{sum}[1;3] \times \operatorname{gcd}[1;3]$ = 8×18 \times 1 = 88;
  • [2;2][2;2] --- $\operatorname{sum}[2;2] \times \operatorname{gcd}[2;2]$ = 3×33 \times 3 = 99;
  • [2;3][2;3] --- $\operatorname{sum}[2;3] \times \operatorname{gcd}[2;3]$ = 5×15 \times 1 = 55;
  • [3;3][3;3] --- $\operatorname{sum}[3;3] \times \operatorname{gcd}[3;3]$ = 2×22 \times 2 = 44.

Scoring

  • (44 points): n3n \le 3;
  • (88 points): n,q103n, q \le 10^3;
  • (55 points): n103n \le 10^3;
  • (1717 points): n,q105n, q \le 10^5;
  • (1414 points): n105n \le 10^5;
  • (55 points): ai20a_i \le 20;
  • (77 points): ai103a_i \le 10^3;
  • (1616 points): l=1l = 1;
  • (2424 points): no additional constraints.