PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000268. Tổng GCD trên hình chữ nhật (GCD OF MATRIX – Hard)
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 GCD trên hình chữ nhật (GCD OF MATRIX – Hard)
GCD OF MATRIX (hard)
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Consider a matrix with rows and columns. Rows and columns are indexed from . The entry at row and column is , the greatest common divisor of the two indices.
Process rectangle queries. A query represents the inclusive rectangle whose upper-left corner is and lower-right corner is . Return the sum of all matrix entries inside this rectangle, modulo . The matrix never changes.
Input
- The first line contains : the number of rows, columns and queries.
- Each of the next lines contains for one query, in order.
Output
Print exactly lines. Line contains the sum of the entries in the rectangle specified by query , modulo .
Subtasks
- Subtask 1 (20 points): ; ; for each query, and .
- Subtask 2 (30 points): ; ; for each query, and .
- Subtask 3 (50 points): ; ; for each query, and .
Examples
Input
3 2 2
1 1 2 2
2 1 3 2
Output
5
5
Explanation
The three rows contain , and , respectively, as , , , . The first rectangle has sum . The second has sum . Thus both output lines contain 5.