Kuroni and Impossible Calculation CodeForces - 1305C(鸽巢原理)

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

If you are not familiar with short notation, ∏1≤i

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

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.
思路:鸽巢原理真的很神奇,有的时候很麻烦的一件事突然就很明朗了。
如果n>m,那么一定有两个数字取余m相等,那么他们相减之后就肯定为0,最终结果也为0.这样就只暴力考虑1000之内的情况就可以了。
代码如下:

#include
#define ll long long
using namespace std;

const int maxx=2e5+100;
int a[maxx];
int n,m;

int main()
{
	scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) cin>>a[i];
		//for(int i=1;i<=n;i++) cout<
		if(n>m) cout<<0<<endl;
		else
		{
			ll ans=1ll;
			for(int i=1;i<=n;i++)
			{
				for(int j=i+1;j<=n;j++) ans=(ans*(ll)abs(a[i]-a[j]))%m;
			}
			cout<<ans%m<<endl;
			//for(m=1;m<=1000;m++) cout<
		}
		
	return 0;
}

努力加油a啊,(o)/~

你可能感兴趣的:(Kuroni and Impossible Calculation CodeForces - 1305C(鸽巢原理))