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

#FT0000207. Tổng các giá trị nhỏ nhất (Sum of Min)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng các giá trị nhỏ nhất (Sum of Min)

Sum of Min

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given positive integers N,MN,M and two sequences of positive integers A=(A0,…,AN−1)A=(A_0,\ldots,A_{N-1}), B=(B0,…,BM−1)B=(B_0,\ldots,B_{M-1}), note that both sequences are indexed from 00.

For every integer i=0,1,…,K−1i=0,1,\ldots,K-1, consider the pair (Ai mod N,Bi mod M)(A_{i\bmod N},B_{i\bmod M}), where i mod ti\bmod t is the remainder after division by tt. The indices repeat periodically with their respective sequence lengths.

Compute

$$S=\sum_{i=0}^{K-1}\min\left(A_{i\bmod N},B_{i\bmod M}\right),$$

and print SS modulo 998244353998244353. The value of KK can be very large.

Input

  • The first line contains integers N,M,KN,M,K, the two sequence lengths and the number of terms in the sum.
  • The second line contains exactly NN integers A0,A1,…,AN−1A_0,A_1,\ldots,A_{N-1} in increasing index order.
  • The third line contains exactly MM integers B0,B1,…,BM−1B_0,B_1,\ldots,B_{M-1} in increasing index order.

Output

Print one integer: the value of SS modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 1≤N,M≤81\le N,M\le 8; 1≤K≤2501\le K\le 250; 1≤Ai,Bj≤1091\le A_i,B_j\le 10^9 at every valid index.
  • Subtask 2 (30 points): 1≤N,M≤30001\le N,M\le 3000; 1≤K≤1071\le K\le 10^7; 1≤Ai,Bj≤1091\le A_i,B_j\le 10^9 at every valid index.
  • Subtask 3 (50 points): 1≤N,M≤2⋅1051\le N,M\le 2\cdot10^5; 1≤K≤10181\le K\le 10^{18}; 1≤Ai,Bj≤1091\le A_i,B_j\le 10^9 at every valid index.

Examples

Input

3 2 5
3 1 4
1 5

Output

7

Explanation

Here N=3,M=2,K=5N=3,M=2,K=5. For i=0,1,2,3,4i=0,1,2,3,4, the chosen pairs are (3,1)(3,1), (1,5)(1,5), (4,1)(4,1), (3,5)(3,5), and (1,1)(1,1), respectively. Their minima are 1,1,1,3,11,1,1,3,1, so the sum is 1+1+1+3+1=71+1+1+3+1=7. Reducing modulo 998244353998244353 does not change it.