Codeforces Round #609 (Div. 2) B. Modulo Equality

这是一个关于编程竞赛的问题,目标是找到最小的整数x,使得整数序列a中的每个数加上x并进行模m运算后,重新排序的序列与序列b相等。通过输入的数组大小n和模数m,以及两个整数序列a和b,要求求解满足条件的x。题目保证存在这样的非负整数x,可以通过某种排列使得(a_i + x) mod m = b_p_i。给出的样例展示了如何通过枚举x来解决这个问题。
摘要由CSDN通过智能技术生成

有两个整数序列a = [a1, a2, ..., an],b = [b1, b2, ..., bn],长度都为n,找到一个最小的数x,使得a中的每个数加上x后对m取模,然后重新排序序列a,使得a = b。

Input

第一行为n,m (1≤n≤2000,1≤m≤10^9),分别是数组的大小和m。

第二行为数组a的所有元素(0≤ai<m ).

第二行为数组b的所有元素 (0≤bi<m ).

It is guaranteed that there exists some non-negative integer xx , such that it would be possible to find some permutation p1,p2,…,pnp1,p2,…,pn such that (ai+x)modm=bpi(ai+x)modm=bpi .

Output

输出可以加上满足(ai+x) mod m=b 的最小x 。

Input

4 3
0 0 2 1
2 0 1 1

Output

1

Input

3 2
0 0 0
1 1 1

Output

1

Input

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

Output

0

由于a数组中肯定一个数会与b数组中一个数配对,所以我们暴力枚举x,直至找到满足条件的x。

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn = 2005;
int n, m;
ll a[maxn],b[maxn],c[maxn];

bool ok()
{
    for (int i = 1; i <= n; i++)
    {
        if (c[i] != b[i])
            return 0;
    }
    return 1;
}

int main()
{
    ll ans=0x3f3f3f3f;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &b[i]);
    sort(b + 1, b + 1 + n);
    for (int i = 1; i <= n; i++)
    {
        ll x = a[1] <= b[i] ? (b[i] - a[1]) : (b[i] + m - a[1]);
        for (int j = 1; j <= n; j++)
        {
            c[j] = (a[j] + x) % m;
        }
        sort(c + 1, c + 1 + n);
        if (ok())
            ans = min(ans, x);
    }
    printf("%lld", ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值