#P7496. 干杯!再会!

    ID: 6282 远端评测题 1500ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学2021O2优化最大公约数,gcd莫比乌斯反演

干杯!再会!

Description

This small shop has nn regular customers. When the ii-th customer comes to the shop, they bring a base wine with deliciousness aia_i and a seasoning with deliciousness bib_i. These customers ask Demi to mix the wine for them. For a base wine with deliciousness xx and a seasoning with deliciousness yy, if Demi mixes them together, she can obtain a bottle of fine wine with deliciousness gcd(x,y)\gcd(x,y) (we consider that a lower deliciousness value means the wine tastes better).

On this day, these customers came to the shop at the same time and wanted Demi to mix the wine. However, Demi drank too much the day before and became confused, which caused her to add the seasonings to the wrong base wines. Fortunately, the customers do not mind. They only want to know, for all possible ways Demi adds the seasonings, what the result is of the sum of the variances of the deliciousness values of the wines they will get, taken modulo 109+710^9+7. If you can answer their question, they will be very willing to help you pay for the drinks.


Brief statement:

Given nn and two sequences a,ba, b of length nn. For a permutation pp of 11 to nn, define ci=gcd(ai,bpi)c_i=\gcd(a_i,b_{p_i}). Let σ(c)\sigma(c) denote the variance of all elements in sequence cc (see the variance formula in the hint). Compute:

pσ(c)\sum\limits_{p}\sigma(c)

modulo 109+710^9+7.

Input Format

The first line contains an integer nn, as described above.

The second line contains nn integers; the ii-th integer is aia_i.

The third line contains nn integers; the ii-th integer is bib_i.

Output Format

Output one integer on one line, the answer.

3
1 2 3
1 2 3

777777785
12
1 3 4 2 3 5 7 3 5 6 8 9
4 3 10 2 5 6 4 8 2 9 12 5

931089600

Hint

Explanation for Sample 1

  • p={1,2,3},c={1,2,3},σ(c)=23p=\{1,2,3\},c=\{1,2,3\},\sigma(c)=\dfrac{2}{3}.
  • p={1,3,2},c={1,1,1},σ(c)=0p=\{1,3,2\},c=\{1,1,1\},\sigma(c)=0.
  • p={2,1,3},c={1,1,3},σ(c)=89p=\{2,1,3\},c=\{1,1,3\},\sigma(c)=\dfrac{8}{9}.
  • p={2,3,1},c={1,1,1},σ(c)=0p=\{2,3,1\},c=\{1,1,1\},\sigma(c)=0.
  • p={3,1,2},c={1,1,1},σ(c)=0p=\{3,1,2\},c=\{1,1,1\},\sigma(c)=0.
  • p={3,2,1},c={1,2,1},σ(c)=29p=\{3,2,1\},c=\{1,2,1\},\sigma(c)=\dfrac{2}{9}.

The total sum is 169\dfrac{16}{9}, which is 777777785777777785 modulo 109+710^9+7.


Constraints

This problem uses bundled testdata.

  • Subtask 1 ( 5%5\% ): n8n\leq8.
  • Subtask 2 ( 15%15\% ): n,ai,bi100n,a_i,b_i\leq100.
  • Subtask 3 ( 25%25\% ): ai,bi103a_i,b_i\leq10^3.
  • Subtask 4 ( 25%25\% ): n,ai,bi105n,a_i,b_i\leq 10^5.
  • Subtask 5 ( 30%30\% ): no special constraints.

For all testdata: 2n106,1ai,bi1062\leq n\leq 10^6,1\leq a_i,b_i\leq 10^6.


For a sequence xx of length nn, the variance is $\sigma(x)=\sum\limits_{i=1}^n\dfrac{1}{n}(x_i-\bar{x})^2$, where xˉ\bar{x} is the average of all elements (xˉ=1ni=1nxi\bar{x}=\dfrac{1}{n}\sum\limits_{i=1}^nx_i).

Translated by ChatGPT 5