[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) ab(mod m)那么 ( a − b ) ≡ 0 ( m o d   m ) (a - b) \equiv 0(mod \ m) (ab)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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值