#P12157. Magic Incantation Combination

    ID: 14258 Type: Default 1000ms 256MiB

Magic Incantation Combination

Magic Incantation Combination

In this problem, you are given two lists of integers representing two parts of a magical incantation. The first list contains the upper parts a1,a2,,ana_1, a_2, \dots, a_n and the second list contains the lower parts b1,b2,,bmb_1, b_2, \dots, b_m. A complete incantation is formed by choosing one upper part and one lower part, and its value is defined as $$S = a_i + b_j.$$

A spell is considered valid if it satisfies both of the following conditions:

  • $$S \leq n+m$$
  • $$S$$ is a prime number
The type of magic depends solely on the value of $$S$$. Since each upper and lower incantation can be used in different combinations, your task is to determine the number of distinct valid spells (i.e. distinct values of $$S$$ that satisfy the conditions).

inputFormat

The first line contains two integers $$n$$ and $$m$$, representing the number of upper and lower incantations respectively.

The second line contains $$n$$ integers, the upper incantations: $$a_1, a_2, \dots, a_n$$.

The third line contains $$m$$ integers, the lower incantations: $$b_1, b_2, \dots, b_m$$.

outputFormat

Output a single integer, which is the number of distinct valid spells (i.e. distinct values of $$S$$ such that $$S \leq n+m$$ and $$S$$ is a prime number).

sample

3 3
1 2 3
1 2 3
3