To become the king of Codeforces, Kuroni has to solve the following problem.
He is given nn numbers a1,a2,…,ana1,a2,…,an. Help Kuroni to calculate ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj|. As result can be very big, output it modulo mm.
If you are not familiar with short notation, ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj| is equal to |a1−a2|⋅|a1−a3|⋅|a1−a2|⋅|a1−a3|⋅ …… ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ …… ⋅|a2−an|⋅⋅|a2−an|⋅ …… ⋅|an−1−an|⋅|an−1−an|. In other words, this is the product of |ai−aj||ai−aj| for all 1≤i<j≤n1≤i<j≤n.
The first line contains two integers nn, mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤10001≤m≤1000) — number of numbers and modulo.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109).
Output the single number — ∏1≤i<j≤n|ai−aj|modm∏1≤i<j≤n|ai−aj|modm.
2 10
8 5
3
3 12
1 4 5
0
3 7
1 4 9
1
In the first sample, |8−5|=3≡3mod10|8−5|=3≡3mod10.
In the second sample, |1−4|⋅|1−5|⋅|4−5|=3⋅4⋅1=12≡0mod12|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|1−4|⋅|1−9|⋅|4−9|=3⋅8⋅5=120≡1mod7.
把n转换到m上去,注意一下模运算与绝对值的关系
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll long long
#define PII pair<int, int>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dec(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mk make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 100007;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)
inline ll read()
{
ll x = 0; bool f = true; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? x : -x;
}
ll gcd(ll m, ll n)
{
return n == 0 ? m : gcd(n, m % n);
}
int main()
{
int n, m;
cin >> n >> m;
vector<ll> a(n),b(n);
vector<bool> vis(N, false);
for (int i = 0; i < n; i++)
{
a[i]=read();
b[i] = a[i];
a[i] %= m;
if (vis[a[i]])
{
cout << 0 << endl;
return 0;
}
vis[a[i]] = 1;
}
ll ans = 1;
int tmp = 1;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (b[i] < b[j])
tmp *= -1;
ans = ans*(a[i] - a[j]+m) % m;
}
}
cout << (tmp*ans+m)%m << endl;
return 0;
}