[CodeForces - 1305C] - Kuroni and Impossible Calculation【同餘+鴿巢】

[CodeForces - 1305C] - Kuroni and Impossible Calculation

題意:

  • 一個長度爲n的序列,我們可以得到任意兩個數差的絕對值,所有絕對值相乘對m取模的結果?

思路:

(1)同餘定理:

如果ab(mod m)a \equiv b(mod \ m)那麼(ab)0(mod m)(a - b) \equiv 0(mod \ m)

(2)鴿巢原理(抽屜原理)

將十個蘋果放在九個抽屜裏,無論怎樣放,總會有一個抽屜裏有兩個蘋果。

  • 利用上面兩個定理。我們很容易可以得到,如果n>m,那麼總有兩個數對於m同餘,所以最後的乘積也總是爲0. 反之,n <= m,那麼範圍只有1000,暴力求解即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

const int maxN = 200005;

int n, m, a[maxN];

int main()
{
    n = read(); m = read();
    for(int i = 0; i < n; ++ i )
        a[i] = read();
    if(n > m)
        cout << "0\n";
    else
    {
        ll 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;
        cout <<  ans % m << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章