Deposits(ACM ICPC 2008–2009, NEERC, Northern Subregional Contest Problem D)

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值