| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 14813 | Add two large integers |
|
| 14814 | Count IPs in the same subnet |
|
Description
Write a program to compute the sum of two positive integers.
Each integer may contain up to n digits, where 0<n≤18.
Because the integers can be very large, an appropriate data type should be used to correctly perform the addition.
Input
Two positive integers a and b, each with at most 18 digits.
Output
One integer representing the sum of a and b.
Ensure that the output, including formatting 輸出格式, exactly matches the provided samples.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
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.
Input
-
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.
Output
One integer — the number of IP addresses that belong to the same subnet as A.