codeforces C. Kuroni and Impossible Calculation

C. Kuroni and Impossible Calculation

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 < j ≤ n ∣ a i − a j ∣ m o d m \prod_{1≤i1i<jnaiajmodm.

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.

题意:

计算出 ∏ 1 ≤ i < j ≤ n ∣ a i − a j ∣ m o d m \prod_{1≤i1i<jnaiajmodm.的值。

思路:

如果用纯暴力的话2*105肯定会超时,但是这里有一个鸽巢定理和取模运算,因为((a % mod - b % mod) + mod) % mod = (a - b + mod) % mod所以把全部数取余后,加入n超过了m的话,那么一定有一个数重复,重复的话,相减就为0,那么取模一定为0,这就是鸽巢原理,如果不懂那么点这里,这样最坏的情况就是O(10002)然后暴力就可以了。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 2e+5 + 10;
int a[maxn];
int main() {
    int n, m;
    read(n), read(m);
    for (int i = 0; i < n; i++) read(a[i]);
    if (n > m) {
        printf("0");
        return 0;
    }
    int ans = 1;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            ans = (ans * (abs(a[i] - a[j]) % m)) % m;
        }
    }
    printf("%d", ans);
    return 0;
}

你可能感兴趣的:(codeforces)