#P5425. [USACO19OPEN] I Would Walk 500 Miles G

[USACO19OPEN] I Would Walk 500 Miles G

Description

Farmer John wants to divide his NN cows numbered 1N1 \ldots N (N7500N \leq 7500) into KK non-empty groups (2KN2 \leq K \leq N), such that any two cows from different groups must walk some distance to meet each other. Cows xx and yy (where 1x<yN1 \leq x<y \leq N) are willing to walk (2019201913x+2019201949y)mod2019201997(2019201913x+2019201949y) \mod 2019201997 miles in order to meet.

Given a grouping plan that divides the NN cows into KK non-empty groups, let MM be the minimum number of miles that any two cows from different groups are willing to walk to meet. To test the cows' loyalty to each other, Farmer John wants to divide the NN cows into KK groups in the best possible way so that MM is as large as possible.

Input Format

The input consists of a single line containing NN and KK, separated by a space.

Output Format

Output the optimal value of MM.

3 2
2019201769

Hint

In this example, cow 11 and cow 22 are willing to walk 20192018172019201817 miles to meet. Cow 22 and cow 33 are willing to walk 20192016852019201685 miles. Cow 11 and cow 33 are willing to walk 20192017692019201769 miles. Therefore, if cow 11 is put alone in one group, and cows 22 and 33 are put into another group, then M=min(2019201817,2019201769)=2019201769M = \min(2019201817, 2019201769) = 2019201769 (this is the best result we can achieve in this problem).

Translated by ChatGPT 5