We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. GCD Matrix

GCD Matrix

Problem
Submissions
Leaderboard
Discussions
Editorial

Alex has two arrays defined as and . He created an matrix, , where for each in . Recall that is the greatest common divisor of and .

For example, if and , he builds like so:

Alex's friend Kiara loves matrices, so he gives her questions about matrix where each question is in the form of some submatrix of with its upper-left corner at and its bottom-right corner at . For each question, find and print the number of distinct integers in the given submatrix on a new line.

Input Format

The first line contains three space-separated integers describing the respective values of (the size of array ), (the size of array ), and (Alex's number of questions).
The second line contains space-separated integers describing .
The third line contains space-separated integers describing .
Each line of the subsequent lines contains four space-separated integers describing the respective values of , , , and for the question (i.e., defining a submatrix with upper-left corner and bottom-right corner ).

Constraints

Scoring

  • for of score.
  • for of score.

Output Format

For each of Alex's questions, print the number of distinct integers in the given submatrix on a new line.

Sample Input 0

3 3 3
1 2 3
2 4 6
0 0 1 1
0 0 2 2
1 1 2 2

Sample Output 0

2
3
3

Explanation 0

Given and , we build the following :

The diagram below depicts the submatrices for each of the questions in green:

image

  1. For the submatrix between and , the set of integers is . The number of distinct integers is .
  2. For the submatrix between and , the set of integers is . The number of distinct integers is .
  3. For the submatrix between and , the set of integers is . The number of distinct integers is .

Author

ma5termind

Difficulty

Hard

Max Score

60

Submitted By

1646

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

Join us

Create a HackerRank account

Be part of a 26 million-strong community of developers

Please signup or login in order to view this challenge

or
Already have an account?Log in