CF291C.Network Mask
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively.
Polycarpus has found a job, he is a system administrator. One day he came across n IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separated by dots. For example, the record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In this problem an arbitrary group of four 8-bit numbers is a correct IP address.
Having worked as an administrator for some time, Polycarpus learned that if you know the IP address, you can use the subnet mask to get the address of the network that has this IP addess.
The subnet mask is an IP address that has the following property: if we write this IP address as a 32 bit string, that it is representable as "11...11000..000". In other words, the subnet mask first has one or more one bits, and then one or more zero bits (overall there are 32 bits). For example, the IP address 2.0.0.0 is not a correct subnet mask as its 32-bit record looks as 00000010000000000000000000000000.
To get the network address of the IP address, you need to perform the operation of the bitwise "and" of the IP address and the subnet mask. For example, if the subnet mask is 255.192.0.0, and the IP address is 192.168.1.2, then the network address equals 192.128.0.0. In the bitwise "and" the result has a bit that equals 1 if and only if both operands have corresponding bits equal to one.
Now Polycarpus wants to find all networks to which his IP addresses belong. Unfortunately, Polycarpus lost subnet mask. Fortunately, Polycarpus remembers that his IP addresses belonged to exactly k distinct networks. Help Polycarpus find the subnet mask, such that his IP addresses will belong to exactly k distinct networks. If there are several such subnet masks, find the one whose bit record contains the least number of ones. If such subnet mask do not exist, say so.
本题采用简化的 TCP/IP 地址模型,请务必仔细阅读题干。
波利卡普斯找到了一份工作,担任系统管理员。某天,他遇到了 n 个 IP 地址。每个 IP 地址是一个 32 位整数,表示为四组 8 位数字(不含前导零),各组之间用点号分隔。例如,0.255.1.123 是一个合法的 IP 地址,而 0.256.1.123 和 0.255.1.01 则不合法。在本题中,任意一组四个 8 位数字均视为合法的 IP 地址。
在担任管理员一段时间后,波利卡普斯了解到:若已知某个 IP 地址,则可通过子网掩码(subnet mask)计算出该 IP 地址所属的网络地址。
子网掩码本身也是一个 IP 地址,具有如下性质:将其写成 32 位二进制字符串时,其形式为 "11...11000..000"。换言之,子网掩码的 32 位二进制表示中,前面是一段(至少一位)连续的 1,后面是一段(至少一位)连续的 0(总共 32 位)。例如,IP 地址 2.0.0.0 不是合法的子网掩码,因为其 32 位二进制表示为 00000010000000000000000000000000。
要得到某 IP 地址对应的网络地址,需对该 IP 地址与子网掩码执行按位“与”(bitwise AND)运算。例如,若子网掩码为 255.192.0.0,IP 地址为 192.168.1.2,则网络地址为 192.128.0.0。在按位“与”运算中,结果的某一位为 1 当且仅当两个操作数在该位上均为 1。
现在,波利卡普斯希望找出他所拥有的所有 IP 地址各自所属的网络。不幸的是,他丢失了子网掩码;幸运的是,他记得这些 IP 地址恰好属于 k 个互不相同的网络。请帮助波利卡普斯找出一个子网掩码,使得这些 IP 地址恰好划分到 k 个互不相同的网络中。如果存在多个满足条件的子网掩码,则选择其 32 位二进制表示中 1 的个数最少的那个。若不存在这样的子网掩码,请说明这一点。
输入格式
The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 105) — the number of IP addresses and networks. The next n lines contain the IP addresses. It is guaranteed that all IP addresses are distinct.
第一行包含两个整数 n 和 k(1≤k≤n≤105)——分别为 IP 地址的数量和网络的数量。接下来的 n 行包含 IP 地址。保证所有 IP 地址互不相同。
输出格式
In a single line print the IP address of the subnet mask in the format that is described in the statement, if the required subnet mask exists. Otherwise, print -1.
在一行中输出子网掩码的 IP 地址,格式如题面所述;如果所需的子网掩码不存在,则输出 -1。
输入输出样例
输入#1
5 3 0.0.0.1 0.1.1.2 0.0.2.1 0.1.1.0 0.0.2.3
输出#1
255.255.254.0
输入#2
5 2 0.0.0.1 0.1.1.2 0.0.2.1 0.1.1.0 0.0.2.3
输出#2
255.255.0.0
输入#3
2 1 255.0.0.1 0.0.0.2
输出#3
-1
输入解题思路,AI测评打分。不知道怎么写?