Programming contests

ECN programozó csapatverseny, 2023. november 25.

November 25, 2023 10:30 AM – November 25, 2023 3:30 PM

Modputer

Modputer is a strange mathematical computing machine that works as follows. The machine has an internal state, which is represented by an array A of N positive integers. The input of the machine is a list of M positive integers, each greater than 1.

The machine reads the integers on the input one by one. For each integer D in the input, the state of the machine gets updated: every integer in the state array which is divisible by D increases by one.

It is difficult to simulate the transitions of Modputer using a regular computer. Your task is to count the number of times each integer in the state array got updated while processing the input of the machine and report the total number of changes.

More formally, let A[i] denote the value of the ith element of the state array before reading the input, and B[i] denote the value of the same element after processing the input. You must calculate the sum of B[i] – A[i] over all i.

Input Specification

The first line of the input contains N and M, the number of elements in the state array and the length of the input list (1 ≤ NM ≤ 100 000).

Each of the next N lines contains an integer A[i], the initial values present in the state array (1 ≤ A[i] ≤ 300 000). Each of the next M lines contains an integer D[i], the input values (2 ≤ D[i] ≤ 300 000).

Output Specification

Print one line to the standard output, containing the total number of changes in the values of the state array while processing the input.

Sample Input

  1. 3 5
  2. 10
  3. 11
  4. 12
  5. 2
  6. 11
  7. 4
  8. 13
  9. 2
download as text file

Output for Sample Input

  1. 12
download as text file

Explanation

The initial state array is [10, 11, 12].
After reading the value D[0] = 2, the state changes to [11, 11, 13].
After reading the value D[1] = 11, the state changes to [12, 12, 13].
After reading the value D[2] = 4, the state changes to [13, 13, 13].
After reading the value D[3] = 13, the state changes to [14, 14, 14].
After reading the value D[4] = 2, the state changes to [15, 15, 15].

The first state value was updated 5 times, the second 4 times and the third 3 times. The total number of changes is 5 + 4 + 3 = 12.

University of Debrecen; Faculty of Informatics; v. 03/01/2019