PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000383. Hoán đổi đoạn để tối ưu GCD (Alter the GCD)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hoán đổi đoạn để tối ưu GCD (Alter the GCD)

Alter the GCD

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given two positive integer arrays a1,…,ana_1,\ldots,a_n and b1,…,bnb_1,\ldots,b_n, you must perform exactly one operation: choose 1≤l≤r≤n1\le l\le r\le n and swap aia_i with bib_i for every index ii in [l,r][l,r].

Afterward compute

S=gcd⁡(a1,…,an)+gcd⁡(b1,…,bn).S=\gcd(a_1,\ldots,a_n)+\gcd(b_1,\ldots,b_n).

Find the maximum possible SS and the number of distinct index pairs (l,r)(l,r) achieving it. Different segments count separately even if they produce identical arrays.

PHOJ input convention: Each .in file contains one test case, without a leading test count tt.

Input

The first line contains nn. The second line contains the nn values of aa, and the third line those of bb.

Output

Print the maximum score and the number of maximizing segments, separated by a space.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le12, 1≤ai,bi≤1091\le a_i,b_i\le10^9.

  • Subtask 2 (30 points): 1≤n≤5 0001\le n\le5\,000, 1≤ai,bi≤1091\le a_i,b_i\le10^9.

  • Subtask 3 (50 points): 1≤n≤200 0001\le n\le200\,000, 1≤ai,bi≤1091\le a_i,b_i\le10^9.

Examples

Example 1

Input

2
18 13
15 20

Output

6 1

Explanation

Swapping the entire range [1,2][1,2] produces arrays [15,20][15,20] and [18,13][18,13], whose GCDs are 55 and 11, totaling 66. The other two possible segments give smaller scores, so exactly one segment is optimal.

Example 2

Input

2
13 14
5 8

Output

2 3

Explanation

There are three segments: [1,1][1,1], [2,2][2,2], and [1,2][1,2]. After any of these swaps, both array GCDs equal 11, so all three segments achieve score 22.