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

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

1 lộ trình

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 AA with nn rows and mm columns. Rows and columns are indexed from 11. The entry at row ii and column jj is Ai,j=gcd⁡(i,j)A_{i,j}=\gcd(i,j), the greatest common divisor of the two indices.

Process qq rectangle queries. A query r1,c1,r2,c2r_1,c_1,r_2,c_2 represents the inclusive rectangle whose upper-left corner is (r1,c1)(r_1,c_1) and lower-right corner is (r2,c2)(r_2,c_2). Return the sum of all matrix entries inside this rectangle, modulo 109+710^9+7. The matrix never changes.

Input

  • The first line contains n,m,qn,m,q: the number of rows, columns and queries.
  • Each of the next qq lines contains r1,c1,r2,c2r_1,c_1,r_2,c_2 for one query, in order.

Output

Print exactly qq lines. Line kk contains the sum of the entries in the rectangle specified by query kk, modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤301\le n,m\le 30; 1≤q≤301\le q\le 30; for each query, 1≤r1≤r2≤n1\le r_1\le r_2\le n and 1≤c1≤c2≤m1\le c_1\le c_2\le m.
  • Subtask 2 (30 points): 1≤n,m≤100001\le n,m\le 10000; 1≤q≤10001\le q\le 1000; for each query, 1≤r1≤r2≤n1\le r_1\le r_2\le n and 1≤c1≤c2≤m1\le c_1\le c_2\le m.
  • Subtask 3 (50 points): 1≤n,m≤10000001\le n,m\le 1000000; 1≤q≤500001\le q\le 50000; for each query, 1≤r1≤r2≤n1\le r_1\le r_2\le n and 1≤c1≤c2≤m1\le c_1\le c_2\le m.

Examples

Input

3 2 2
1 1 2 2
2 1 3 2

Output

5
5

Explanation

The three rows contain (1,1)(1,1), (1,2)(1,2) and (1,1)(1,1), respectively, as gcd⁡(1,1)=gcd⁡(1,2)=1\gcd(1,1)=\gcd(1,2)=1, gcd⁡(2,1)=1\gcd(2,1)=1, gcd⁡(2,2)=2\gcd(2,2)=2, gcd⁡(3,1)=gcd⁡(3,2)=1\gcd(3,1)=\gcd(3,2)=1. The first rectangle has sum 1+1+1+2=51+1+1+2=5. The second has sum 1+2+1+1=51+2+1+1=5. Thus both output lines contain 5.