#P2065. Card Grouping Game

    ID: 15347 Type: Default 1000ms 256MiB

Card Grouping Game

Card Grouping Game

You are given ( m ) blue cards and ( n ) red cards on a table. Each card has an integer greater than ( 1 ). You will pick some cards from the table in several rounds. In each round, you must pick a group of cards such that the cards have different colors (i.e. one blue and one red) and the greatest common divisor (( \gcd )) of the numbers on the cards is greater than ( 1 ).

The task is to compute the maximum number of rounds (or groups) you can form if each card can only be used once. This can be modeled as a bipartite matching problem, where there exists an edge between a blue card and a red card if ( \gcd(blue, red) > 1 ).

inputFormat

The first line contains two integers ( m ) and ( n ) representing the number of blue and red cards respectively. The second line contains ( m ) integers representing the numbers on the blue cards. The third line contains ( n ) integers representing the numbers on the red cards.

outputFormat

Output a single integer indicating the maximum number of groups (rounds) that can be formed under the given conditions.

sample

3 3
2 3 4
8 9 6
3