cswork / OA打开练习工作台

OpenAIOA #9

IP Address to CIDR Blocks

估计中等可在线判题

考点(AI 标注)位运算

Given a starting IPv4 address and a positive integer count, use the minimum number of CIDR blocks to exactly cover the count consecutive addresses beginning at the start address. Over-coverage, gaps, overlaps, and duplicates are not allowed. The blocks may be in any order.

输入格式

The first line contains the start address a.b.c.d, and the second line contains count. Each part is 0..255, written in standard decimal without leading zeros. 1≤count≤4294967296, and the integer value of the start plus count is guaranteed to be ≤4294967296, so the range stays within the IPv4 address space.

输出格式

The first integer k is the number of blocks, followed by k entries a.b.c.d/prefix, which may be output in any order. prefix is in 0..32, each address is the network address of its block (host bits are 0), and decimals have no leading zeros. The cover must be exact and use the minimum number of blocks.

样例 1

输入:

0.0.0.0
1

输出:

1
0.0.0.0/32

样例 2

输入:

0.0.0.0
256

输出:

1
0.0.0.0/24

样例 3

输入:

255.0.0.7
10

输出:

3
255.0.0.7/32
255.0.0.8/29
255.0.0.16/32

样例说明

The first two examples give 0.0.0.0/32 and 0.0.0.0/24 respectively. The third can be split into 255.0.0.7/32, 255.0.0.8/29, 255.0.0.16/32, for 3 blocks, in any order.