An IPv4 address consists of 32 bits, often written as four 8-bit integers (octets), such as 192.168.61.82.
To determine whether two IP addresses belong to the same subnet, a subnet mask is used.
A subnet mask is an IP address whose most significant bits are 1 and the remaining bits are 0.
For example, if the first 26 bits are 1 and the last 6 bits are 0, the mask is 255.255.255.192.
To check if two IP addresses are in the same subnet:
Perform a bitwise AND (&) operation between each IP address and the subnet mask.
If the resulting network identifiers are equal, the two IPs are in the same subnet.
For instance, in Sample Input 1:
IP1: 140 112 28 28 IP2: 140 112 28 8 Mask: 255 255 255 0
Applying the mask:
IP1 & Mask → 140 112 28 0 IP2 & Mask → 140 112 28 0
Since both results are identical (140 112 28 0), the two IPs are in the same subnet.
The input gives:
The first line: the subnet mask (four integers).
The second line: IP address A.
The following lines: several IP addresses to compare with A.
Your task is to count how many of these IPs belong to the same subnet as IP A.
Line 1: four integers representing the subnet mask.
Line 2: four integers representing IP address A.
Lines 3 and beyond: multiple IP addresses to compare (each consists of four integers).
The input ends with EOF.
Each part of an IP satisfies 0≤n<255.
One integer — the number of IP addresses that belong to the same subnet as A.