# GCD Matrix

### Problem Statement :

```Alex has two arrays defined as A = [a0,a1,...,an-1] and B = [b0,b1,...,bm-1]. He created an n*m matrix, M, where Mij = gcd(ai,bj) for each i,j in M. Recall that gcd(a,b) is the greatest common divisor of a and b.

For example, if A= [2,3] and b = [5,6], he builds M = [[1,2], [1,3]] like so:

Alex's friend Kiara loves matrices, so he gives her q questions about matrix M where each question is in the form of some submatrix of M with its upper-left corner at M(r1,c1) and its bottom-right corner at M(r2,c2). 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 n (the size of array A),  m(the size of array B), and q (Alex's number of questions).
The second line contains  nspace-separated integers describing a0,a1,...,an-1.
The third line contains m space-separated integers describing b0,b1,...,bm-1.
Each line i of the q subsequent lines contains four space-separated integers describing the respective values of r1, c1, r2, and c2 for the ith question (i.e., defining a submatrix with upper-left corner (r1,c1) and bottom-right corner (r2,c2)).

Constraints
1 <= n,m <= 10^5
1 <= ai,bi <= 10^5
1 <= q <= 10
0 <= r1,r2 < n
0 <= c1,c2 < m```

### Solution :

```                            ```Solution in C :

In C++ :

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <assert.h>
#include <time.h>
#include <complex.h>

#include <fstream>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, b, a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define Pi 3.14159265358979

typedef long long Int;
typedef unsigned long long UInt;
typedef vector<int> VI;
typedef pair<Int, Int> PII;

const int INF = 1000000000;
const int MAX = 100007;
const int MAXD = 20;
const int MOD = 1000000007;

int a[MAX];
int b[MAX];
int ca[MAX];
int cb[MAX];

Int A[MAX];

int main()
{
//freopen("in.txt" , "r" , stdin);
//freopen("out.txt" , "w" , stdout);

int n , m;
cin >> n >> m;

int q;
cin >> q;

FOR(i,0,n) cin >> a[i];
FOR(i,0,m) cin >> b[i];

FOR(i,0,q)
{
int r1,c1,r2,c2;
cin >> r1 >> c1 >> r2 >> c2;
FILL(ca , 0);
FILL(cb , 0);

FOR(i,r1,r2 + 1)
ca[a[i]] ++;
FOR(i,c1,c2 + 1)
cb[b[i]] ++;

FILL(A, 0);
FOR(i,1,MAX)
{
int cntA = 0;
int cntB = 0;
for(int j = i; j < MAX; j += i)
{
cntA += ca[j];
cntB += cb[j];
}
A[i] = 1LL * cntA * cntB;
}
int res = 0;
RFOR(i,MAX,1)
{
for(int j = 2 * i; j < MAX; j += i)
A[i] -= A[j];
if (A[i]) ++ res;
}
cout << res << endl;

}

return 0;
}

In Java :

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class C2 {
InputStream is;
PrintWriter out;
String INPUT = "";

void solve()
{
int n = ni(), m = ni(), Q = ni();
int[] a = na(n);
int[] b = na(m);
for(int z = 0;z < Q;z++){
int dist = 0;
int r1 = ni(), c1 = ni(), r2 = ni(), c2 = ni();
int[] fr = make(a, r1, r2);
int[] fc = make(b, c1, c2);
long[] f = new long[100005];
for(int i = 0;i < 100005;i++)f[i] = (long)fr[i] * fc[i];
for(int i = 100004;i >= 1;i--){
for(int j = 2*i;j < 100005;j+=i){
f[i] -= f[j];
}
}
for(int i = 0;i < 100005;i++){
assert f[i] >= 0;
if(f[i] > 0)dist++;
}
out.println(dist);
}
}

int[] make(int[] a, int l, int r)
{
int[] f = new int[100005];
for(int i = l;i <= r;i++){
for(int d = 1;d*d <= a[i];d++){
if(a[i] % d == 0){
f[d]++;
if(d*d < a[i])f[a[i]/d]++;
}
}
}
return f;
}

public static long gcd3(long a, long b) {
if(a == 0)return b;
if(b == 0)return a;

int ashift = Long.numberOfTrailingZeros(a);
int bshift = Long.numberOfTrailingZeros(b);
a >>>= ashift;
b >>>= bshift;
while(b != a){
if(a > b){
long t = a; a = b; b = t;
}
b -= a;
b >>>= Long.numberOfTrailingZeros(b);
}

return a<<Math.min(ashift, bshift);
}

void run() throws Exception
{
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);

long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}

public static void main(String[] args) throws Exception {
new C2().run(); }

private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;

{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); }
catch (IOException e) {
throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}

private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126); }
private int skip() {
int b; while((b = readByte()) != -1 && isSpaceChar(b));
return b; }

private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }

private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
// when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
}
return sb.toString();
}

private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
}
return n == p ? buf : Arrays.copyOf(buf, p);
}

private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}

private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}

private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
}

while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
}

private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
}

while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
}

private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o)); }
}

In C :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int a[100000],b[100000];
long long dp1[100000],dp2[100000],dp[100000];

int main(){
int n,m,q,r1,r2,c1,c2,ans,i,j;
scanf("%d%d%d",&n,&m,&q);
for(i=0;i<n;i++)
scanf("%d",a+i);
for(i=0;i<m;i++)
scanf("%d",b+i);
while(q--){
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
memset(dp,0,sizeof(dp));
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
for(i=r1;i<=r2;i++)
for(j=1;j*j<=a[i];j++)
if(a[i]%j==0){
dp1[j-1]++;
if(j*j!=a[i])
dp1[a[i]/j-1]++;
}
for(i=c1;i<=c2;i++)
for(j=1;j*j<=b[i];j++)
if(b[i]%j==0){
dp2[j-1]++;
if(j*j!=b[i])
dp2[b[i]/j-1]++;
}
for(i=99999,ans=0;i>=0;i--){
dp[i]+=dp1[i]*dp2[i];
if(dp[i]>0){
ans++;
dp[0]-=dp[i];
for(j=2;j*j<=i+1;j++)
if((i+1)%j==0){
dp[j-1]-=dp[i];
if(j*j!=i+1)
dp[(i+1)/j-1]-=dp[i];
}
}
}
printf("%d\n",ans);
}
return 0;
}

In Python3 :

#!/bin/python3

import math
import os
import random
import re
import sys

def f(k):
if gf[k]>=0:
return gf[k]
res=ga[k]*gb[k]
if res==0:
gf[k]=0
return 0
for i in range(k+k,m+1,k):
res-=f(i)
gf[k]=res
return res

if __name__ == '__main__':
nmq = input().split()

n = int(nmq[0])

m = int(nmq[1])

q = int(nmq[2])

a = list(map(int, input().rstrip().split()))

b = list(map(int, input().rstrip().split()))

for q_itr in range(q):
r1C1R2C2 = input().split()

r1 = int(r1C1R2C2[0])

c1 = int(r1C1R2C2[1])

r2 = int(r1C1R2C2[2])

c2 = int(r1C1R2C2[3])

sa=set(a[r1:r2+1])
sb=set(b[c1:c2+1])
na=len(a)
nb=len(b)
mxa=max(sa)
mxb=max(sb)
m=min(mxa,mxb)
mm=max(mxa,mxb)

ga=[0]*(m+1)
gb=[0]*(m+1)
ga[1]=na
gb[1]=nb

for i in range(2,m+1):
for j in range(i,mm+1,i):
if j in sa:
ga[i]+=1
if j in sb:
gb[i]+=1
gf=[-1]*(m+1)

f(1)
r=sum([(x>0) for x in gf])
print(r)```
```

## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

## Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

## Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

## Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

## Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim