CodeForces 1305C-Kuroni and Impossible Calculation(抽屉原理)

To become the king of Codeforces, Kuroni has to solve the following problem.

He is given n numbers a1,a2,…,an. Help Kuroni to calculate ∏1≤i<j≤n|ai−aj|. As result can be very big, output it modulo m.

If you are not familiar with short notation, ∏1≤i<j≤n|ai−aj| is equal to |a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an|. In other words, this is the product of |ai−aj| for all 1≤i<j≤n.

Input

The first line contains two integers n, m (2≤n≤2⋅105, 1≤m≤1000) — number of numbers and modulo.

The second line contains n integers a1,a2,…,an (0≤ai≤109).

Output

Output the single number — ∏1≤i<j≤n|ai−aj|modm.

Examples

Input

2 10
8 5

Output

3

Input

3 12
1 4 5

Output

0

Input

3 7
1 4 9

Output

1

Note

In the first sample, |8−5|=3≡3mod10.

In the second sample, |1−4|⋅|1−5|⋅|4−5|=3⋅4⋅1=12≡0mod12.

In the third sample, |1−4|⋅|1−9|⋅|4−9|=3⋅8⋅5=120≡1mod7.

为了成为Codeforce的王者,Kuroni必须解决以下问题。

给他n个数字a1,a2,…,an。 帮助Kuroni计算∏1≤i <j≤n| ai-aj |。 结果可能非常大,以模m形式输出。

如果您不熟悉缩写符号,,1≤i<j≤n| ai-aj | 等于| a1-a2 |⋅| a1-a3 |⋅…⋅| a1-an |⋅| a2-a3 |⋅| a2-a4 |⋅…⋅| a2-a |⋅…⋅| an-1- an |。 换句话说,这是| ai-aj |的乘积 对于所有1≤i<j≤n。

输入值
第一行包含两个整数n,m(2≤n≤2⋅105,1≤m≤1000)-数字和模数。

第二行包含n个整数a1,a2,…,an(0≤ai≤109)。

输出量
输出单个数字— ∏1≤i <j≤n| ai-aj | modm。


输入

2 10
8 5
输出
3
输入
3 12
1 4 5
输出
0
输入
3 7
1 4 9
输出
1
注意
在第一个样本中,| 8-5 | =3≡3mod10。

在第二个样本中,| 1-4 |⋅| 1-5 |⋅| 4-5 | =3⋅4⋅1=12≡0mod12。

在第三个样本中,| 1-4 |⋅| 1-9 |⋅| 4-9 | =3⋅8⋅5=120≡1mod7。

题目大意:给出一组数字a,比如n等于4时,ai<aj,求| a1-a2 | · … · | a3-a4 | %m的值。

解题思路:只需要判断n和m的关系即可。如果n>m,则一定有两个数%m的值时一样的,也就是最后的答案一定是0。为什么这么说呢,抽屉原理:有大于n个物品放入n个抽屉,则至少有一个抽屉里的东西不少于2个。n>m 有n个数,分别对m取模,则至少有两个数是一样的,即一定有至少两个数同余,同余的数相减一定是m的倍数,所以mod m一定为0。如果n<=m,暴力求解即可。
AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int _max=2e5+50;
using LL = long long;
int a[_max];
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		for(int i=1;i<=n;i++)
		  cin>>a[i];
		LL ans=1;
		if(n>m)
		  ans=0;
		else  
		  for(int i=1;i<=n;i++)
		    for(int j=i+1;j<=n;j++)
		    {
			  ans*=abs(a[i]-a[j])%m;
			  ans%=m;
		    }
		cout<<ans<<endl;
	}
	//system("pause");
	return 0;
}
发布了38 篇原创文章 · 获赞 1 · 访问量 1173

猜你喜欢

转载自blog.csdn.net/aezakmias/article/details/104801178