Deposits
Time limit3sMemory limit256 MB
Count pairs of deposits and requests where the deposit amount is divisible by the request length, using frequency counts and divisor enumeration up to 10^6.
- Level
Medium4 of 10
- Topics
- Math, Number theory, Hash map
- Solved
- No attempts yet
Problem
During a financial crisis, many central banks deposit large amounts of cash into investment and savings banks to provide liquidity and support credit markets.
The central bank of Flatland plans to place deposits on the market. Each deposit is described by its amount .
Banks send requests for deposits to the market. There are currently requests. Each request is described by its length , measured in days.
Market regulations require every deposit to be refinanced by an equal integer amount each day. Therefore a deposit with amount and a request with length match each other if and only if is divisible by .
Given the deposits and the requests, find the number of deposit-request pairs that match.
Input
The first line contains , the number of deposits ().
The second line contains integers ().
The third line contains , the number of requests ().
The fourth line contains integers ().
Output
Print a single integer — the number of matching pairs.
Note
Every request is counted individually, so a deposit can match the same length more than once when that length is requested several times.
For the deposits and the request lengths , the matching (deposit, length) pairs are twice, , twice, , twice, twice, , and — pairs in total.