B. Modulo Equality
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a positive integer mm and two integer sequence: a=[a1,a2,,an]a=[a1,a2,…,an] and b=[b1,b2,,bn]b=[b1,b2,…,bn]. 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 aiai by xx, modulo mm (i.e. you want to change aiai to (ai+x)modm(ai+x)modm), 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=[p1,p2,…,pn], such that for all 1in1≤i≤n, (ai+x)modm=bpi(ai+x)modm=bpi, where ymodmymodm — 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.

Input

The first line contains two integers n,mn,m (1n2000,1m1091≤n≤2000,1≤m≤109): number of elemens in arrays and mm.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai<m0≤ai<m).

The third line contains nn integers b1,b2,,bnb1,b2,…,bn (0bi<m0≤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

Print one integer, the smallest 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 for all 1in1≤i≤n.

Examples

4 3
0 0 2 1
2 0 1 1

  output

1

  题意,就是给两个数组a,b让你在a数组基础上对进行a[i]=(a[i]+x)%m操作使得a数组最后和b数组相同,两个数组相同可以顺序不同,但是元素要相同。

我当时就在傻傻的直接暴力模拟,x++的那种!我这个菜鸡很少那么早打完第一题,脑子就想着暴力,虽然知道,会超时,但是想不到其他的办法了->。

后来看到某位dalao的题解,我才发现我没get到枚举的点啊……因为b数组中的每一个b[i]都能用某个a[i]经过操作得到,这个a[i]可能是a[1],a[2],a[3]->a[i],那么我们就把b[i]定下来,为b[0]我们枚举他和每个a[i]的差,看看是否其他b[i]也可以由相同的差求余后变化来,可能x有多个,取最小的就行了。

比如:

#include<bits/stdc++.h>
using namespace std;
int a[2005];
int b[2005];
int c[2005];
map<int ,int> pc;
map<int ,int>pa;
map<int ,int> pb;
//用map和排序后对比时间复杂度一样都是nlong n
int main(void)
{
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		pa[a[i]]++;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&b[i]);
		pb[b[i]]++;
	}

	int flag=1;
	int x=0x3f3f3f3f;
	for(int i=1;i<=n;i++){
		if(pa[a[i]]!=pb[a[i]])
		{
			flag=0;
			break;
		}
	}//不相等
	if(flag==1)
	{
		printf("0\n");
		return 0;
	}
	//b[1]一定在a中对应一个数
	//那么枚举b[0]->a[i] i?
	int z;
	for(int i=1;i<=n;i++){
		z=b[1]-a[i];
		if(z<0) z+=m;
		flag=0;
		pc.clear();
		for(int j=1;j<=n;j++){
			c[j]=(a[j]+z)%m;
			pc[c[j]]++;
		}
		for(int j=1;j<=n;j++){
			if(pc[c[j]]!=pb[c[j]])
			{
				flag=1;
				break;
			}
		}
		if(flag==0)//找最小的
		{
			x=min(x,z);
		}
	}
	printf("%d\n",x);
	return 0;
}

  

  

12-25 19:53