To become the king of Codeforces, Kuroni has to solve the following problem.
He is given n numbers a 1 , a 2 , … , a n a1,a2,…,an a1,a2,…,an. Help Kuroni to calculate ∏ 1 ≤ i < j ≤ n ∣ a i − a j ∣ ∏1≤i<j≤n\ |ai−aj| ∏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 ∣ a i − a j ∣ ∏1≤i<j≤n\ |ai−aj| ∏1≤i<j≤n ∣ai−aj∣ is equal to ∣ a 1 − a 2 ∣ ⋅ ∣ a 1 − a 3 ∣ ⋅ … ⋅ ∣ a 1 − a n ∣ ⋅ ∣ a 2 − a 3 ∣ ⋅ ∣ a 2 − a 4 ∣ ⋅ … ⋅ ∣ a 2 − a n ∣ ⋅ … ⋅ ∣ a n − 1 − a n ∣ |a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an| ∣a1−a2∣⋅∣a1−a3∣⋅…⋅∣a1−an∣⋅∣a2−a3∣⋅∣a2−a4∣⋅…⋅∣a2−an∣⋅…⋅∣an−1−an∣. In other words, this is the product of ∣ a i − a j ∣ |ai−aj| ∣ai−aj∣ for all 1 ≤ i < j ≤ n 1≤i<j≤n 1≤i<j≤n.
Input
The first line contains two integers
n
,
m
(
2
≤
n
≤
2
⋅
105
,
1
≤
m
≤
1000
)
n, m (2≤n≤2⋅105, 1≤m≤1000)
n,m(2≤n≤2⋅105,1≤m≤1000) — number of numbers and modulo.
The second line contains n integers a 1 , a 2 , … , a n ( 0 ≤ a i ≤ 109 ) a1,a2,…,an (0≤ai≤109) a1,a2,…,an(0≤ai≤109).
Output
Output the single number —
∏
1
≤
i
<
j
≤
n
∣
a
i
−
a
j
∣
m
o
d
m
∏1≤i<j≤n\ |ai−aj|\ mod\ m
∏1≤i<j≤n ∣ai−aj∣ mod m.
输入1:
2 10
8 5
输出1:
3
输入2:
3 12
1 4 5
输出2:
0
输入3:
3 7
1 4 9
输出3:
1
题意: 给一个 n ( n < = 2 e 5 ) n (n<=2e5) n(n<=2e5) 个非负整数的序列,以及正整数 m ( m < = 1000 ) m (m<=1000) m(m<=1000),求 ∏ 1 ≤ i < j ≤ n ∣ a i − a j ∣ m o d m ∏1≤i<j≤n\ |ai−aj|\ mod\ m ∏1≤i<j≤n ∣ai−aj∣ mod m 的值。
思路: 由抽屉原理可知,当 n > m n>m n>m 时,必定存在两个 a i m o d m ai\ mod\ m ai mod m同余,此时答案为 0. 否则 n 小于等于 1000,可以直接暴力。
Code:
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=2e5+100;
ll a[N],ans=1;
int main(){
ll n,m; cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
if(n>m) return cout<<0<<endl,0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
ans=ans*(ll)abs(a[j]-a[i])%m;
cout<<ans<<endl;
return 0;
}