#P5435. 基于值域预处理的快速 GCD

基于值域预处理的快速 GCD

Description

Given nn positive integers a1,a2,,ana_1,a_2,\dots,a_n, and another nn positive integers b1,b2,,bnb_1,b_2,\dots,b_n, you need to compute the greatest common divisor of aia_i and bjb_j for every pair (i,j)(i,j).

It is not hard to see that the output should contain n2n^2 positive integers. To reduce the impact of output on the program’s running efficiency, you only need to output nn lines, with one integer AiA_i per line.

For i[1,n]i\in[1,n], Ai=j=1nijgcd(ai,bj)A_i=\sum_{j=1}^{n}i^j\gcd(a_i,b_j). Since the answer may be too large, you only need to output the result modulo 998,244,353998,244,353.

Input Format

The first line contains one positive integer nn.

The second line contains nn positive integers, representing a1,a2,,ana_1,a_2,\dots,a_n.

The third line contains nn positive integers, representing b1,b2,,bnb_1,b_2,\dots,b_n.

Output Format

Output nn lines. The ii-th line should contain one non-negative integer AiA_i. Its meaning is described in the statement.

5
200 300 300 300 23333
666 666 666 666 123456

16
564
3636
14328
3905

Hint

For 20%20\% of the testdata, 1n5001\leqslant n\leqslant 500.

For 100%100\% of the testdata, $1\leqslant n\leqslant 5000;1\leqslant a_i,b_i\leqslant 10^6$.

Please pay attention to the impact of constant factors on the program’s running efficiency.

Translated by ChatGPT 5