[CodeForces - 1305C] - Kuroni and Impossible Calculation【同余+鸽巢】

[CodeForces - 1305C] - Kuroni and Impossible Calculation

题意:

  • 一个长度为n的序列,我们可以得到任意两个数差的绝对值,所有绝对值相乘对m取模的结果?

思路:

(1)同余定理:

如果 a b ( m o d   m ) a \equiv b(mod \ m) 那么 ( a b ) 0 ( m o d   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;
}
发布了273 篇原创文章 · 获赞 76 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/104683803