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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 and two sequences of positive integers , , note that both sequences are indexed from .
For every integer , consider the pair , where is the remainder after division by . 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 modulo . The value of can be very large.
Input
- The first line contains integers , the two sequence lengths and the number of terms in the sum.
- The second line contains exactly integers in increasing index order.
- The third line contains exactly integers in increasing index order.
Output
Print one integer: the value of modulo .
Subtasks
- Subtask 1 (20 points): ; ; at every valid index.
- Subtask 2 (30 points): ; ; at every valid index.
- Subtask 3 (50 points): ; ; at every valid index.
Examples
Input
3 2 5
3 1 4
1 5
Output
7
Explanation
Here . For , the chosen pairs are , , , , and , respectively. Their minima are , so the sum is . Reducing modulo does not change it.