Input file: deposits.in
Output file: deposits.out
Time limit: 3 seconds
Memory limit: 256 megabytes
Description
Financial crisis forced many central banks deposit large amounts of cash to accounts of investment andsavings banks in order to provide liquidity and save credit markets.
Central bank of Flatland is planning to put n deposits to the market. Each deposit is characterized byits amount ai.
The banks provide requests for deposits to the market. Currently there are m requests for deposits. Eachrequest is characterized by its length bi days.
The regulations of Flatland’s market authorities require each deposit to be refinanced by equal integeramount each day. That means that a deposit with amount a and a request with length b match eachother if and only if a is divisible by b.
Given information about deposits and requests, find the number of deposit-request pairs that match eachother.
Input
The first line of the input file contains n — the number of deposits (1 ≤ n ≤ 100 000). The second linecontains n integer numbers: a1, a2, . . . , an (1 ≤ ai ≤ 106).The third line of the input file contains m — the number of requests (1 ≤ m ≤ 100 000). The forth linecontains m integer numbers: b1, b2, . . . , bm (1 ≤ bi ≤ 106).
Output
Output one number — the number of matching pairs.
Sample Input
4
3 4 5 6
4
1 1 2 3
Sample Output
12
题解:如果数组a的元素可以整除b中元素sum+1,正常做法肯定T,所以借助素数打表的思路,用下标对应数值。
代码如下:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn = 1e6+5;
ll n,m,k,a[maxn],b[maxn];
int main()
{
freopen("deposits.in","r",stdin);
freopen("deposits.out","w",stdout);
while(cin>>n)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0; i<n; i++)
{
cin>>k;
a[k]++;
}
cin>>m;
for(int i=0; i<m; i++)
{
cin>>k;
b[k]++;
}
ll sum=0;
for(int i=0; i<maxn; i++)
{
if(b[i])
{
for(ll j=1;i*j<maxn; j++)
{
sum+=(a[i*j]*b[i]);
}
}
}
cout<<sum<<endl;
}
return 0;
}