http://blog.csdn.net/huayunhualuo/article/details/54944548
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
using namespace std;
const int MAX = 100000+100;
const double eps = 1e-10;
const double PI = acos(-1.0);
int n, m, q, a[MAX], b[MAX];
int r1,c1,r2,c2;
long long aa[MAX], bb[MAX], ans[MAX];
int main()
{
while(~scanf("%d%d%d", &n, &m, &q))
{
for(int i = 0; i<n; ++i)
scanf("%d", &a[i]);
for(int j = 0; j<m; ++j)
scanf("%d", &b[j]);
while(q--)
{
scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
memset(aa, 0, sizeof(aa));
memset(bb, 0, sizeof(bb));
for(int i = r1; i<=r2; ++i)
aa[a[i]]++;
for(int j = c1; j<=c2; ++j)
bb[b[j]]++;
for(int i = 1; i<=100000; ++i)
{
int j = i*2;
while(j<=100000)
{
aa[i]+=aa[j];
bb[i]+=bb[j];
j+=i;
}
}
int anss = 0;
for(int i = 100000; i>=1; --i)
{
ans[i] = aa[i]*bb[i];
int j = i*2;
while(j<=100000)
{
ans[i]-=ans[j];
j+=i;
}
if(ans[i])anss++;
}
cout<<anss<<endl;
}
}
return 0;
}