CF1269B.Modulo Equality

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a positive integer mm and two integer sequence: a=[a1,a2,…,an]a=[a_1, a_2, \ldots, a_n] and b=[b1,b2,…,bn]b=[b_1, b_2, \ldots, b_n]. Both of these sequence have a length nn.

Permutation is a sequence of nn different positive integers from 11 to nn. For example, these sequences are permutations: [1][1], [1,2][1,2], [2,1][2,1], [6,7,3,4,1,2,5][6,7,3,4,1,2,5]. These are not: [0][0], [1,1][1,1], [2,3][2,3].

You need to find the non-negative integer xx, and increase all elements of aia_i by xx, modulo mm (i.e. you want to change aia_i to (ai+x) mod m(a_i + x) \bmod m), so it would be possible to rearrange elements of aa to make it equal bb, among them you need to find the smallest possible xx.

In other words, you need to find the smallest non-negative integer xx, for which it is possible to find some permutation p=[p1,p2,…,pn]p=[p_1, p_2, \ldots, p_n], such that for all 1≤i≤n1 \leq i \leq n, (ai+x) mod m=bpi(a_i + x) \bmod m = b_{p_i}, where y mod my \bmod m — remainder of division of yy by mm.

For example, if m=3m=3, a=[0,0,2,1],b=[2,0,1,1]a = [0, 0, 2, 1], b = [2, 0, 1, 1], you can choose x=1x=1, and aa will be equal to [1,1,0,2][1, 1, 0, 2] and you can rearrange it to make it equal [2,0,1,1][2, 0, 1, 1], which is equal to bb.

给你一个正整数 mm 和两个整数序列:a=[a1,a2,…,an]a=[a_1, a_2, \ldots, a_n] 与 b=[b1,b2,…,bn]b=[b_1, b_2, \ldots, b_n]。这两个序列的长度均为 nn。

排列 是由 11 到 nn 的 nn 个互不相同的正整数组成的序列。例如,以下序列是排列:[1][1]、[1,2][1,2]、[2,1][2,1]、[6,7,3,4,1,2,5][6,7,3,4,1,2,5];而以下序列不是排列:[0][0]、[1,1][1,1]、[2,3][2,3]。

你需要找出一个非负整数 xx,使得将 aa 中每个元素 aia_i 增加 xx 后对 mm 取模(即把 aia_i 变为 (ai+x) mod m(a_i + x) \bmod m),所得的新序列可以通过重新排列其元素而与 bb 完全相等。在所有满足条件的 xx 中,你需要找出最小的那个。

换言之,你需要找出最小的非负整数 xx,使得存在某个排列 p=[p1,p2,…,pn]p=[p_1, p_2, \ldots, p_n],满足对所有 1≤i≤n1 \leq i \leq n,均有 (ai+x) mod m=bpi(a_i + x) \bmod m = b_{p_i},其中 y mod my \bmod m 表示 yy 除以 mm 所得的余数。

例如,若 m=3m=3,a=[0,0,2,1]a = [0, 0, 2, 1],b=[2,0,1,1]b = [2, 0, 1, 1],你可以选择 x=1x=1,此时 aa 变为 [1,1,0,2][1, 1, 0, 2],再将其重排为 [2,0,1,1][2, 0, 1, 1](即 bb),即可满足要求。

输入格式

The first line contains two integers n,mn,m (1≤n≤2000,1≤m≤1091 \leq n \leq 2000, 1 \leq m \leq 10^9): number of elemens in arrays and mm.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<m0 \leq a_i \lt m).

The third line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (0≤bi<m0 \leq b_i \lt m).

It is guaranteed that there exists some non-negative integer xx, such that it would be possible to find some permutation p1,p2,…,pnp_1, p_2, \ldots, p_n such that (ai+x) mod m=bpi(a_i + x) \bmod m = b_{p_i}.

第一行包含两个整数 n,mn,m(1≤n≤2000,1≤m≤1091 \leq n \leq 2000, 1 \leq m \leq 10^9):数组的元素个数和 mm。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai<m0 \leq a_i \lt m)。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(0≤bi<m0 \leq b_i \lt m)。

保证存在某个非负整数 xx,使得存在某个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,满足 (ai+x) mod m=bpi(a_i + x) \bmod m = b_{p_i}。

输出格式

Print one integer, the smallest non-negative integer xx, such that it would be possible to find some permutation p1,p2,…,pnp_1, p_2, \ldots, p_n such that (ai+x) mod m=bpi(a_i + x) \bmod m = b_{p_i} for all 1≤i≤n1 \leq i \leq n.

输出一个整数,即满足条件的最小非负整数 xx:存在某个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,使得对所有 1≤i≤n1 \leq i \leq n 均有 (ai+x) mod m=bpi(a_i + x) \bmod m = b_{p_i}。

输入输出样例

  • 输入#1

    4 3
    0 0 2 1
    2 0 1 1

    输出#1

    1
  • 输入#2

    3 2
    0 0 0
    1 1 1

    输出#2

    1
  • 输入#3

    5 10
    0 0 0 1 2
    2 1 0 0 0

    输出#3

    0

输入解题思路,AI测评打分。不知道怎么写?

首页