前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

作者头像
风骨散人Chiam
发布2020-10-28 10:20:49
2500
发布2020-10-28 10:20:49
举报
文章被收录于专栏:CSDN旧文

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<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|ai−aj| is equal to |a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an|. In other words, this is the product of |ai−aj| for all 1≤i<j≤n.

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|ai−aj|modm.

Examples inputCopy 2 10 8 5 outputCopy 3 inputCopy 3 12 1 4 5 outputCopy 0 inputCopy 3 7 1 4 9 outputCopy 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.

这就是个鸽笼原理,m<=1000

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
    char ch = getchar();
    x = 0;
    t f = 1;
    while (ch < '0' || ch > '9')
        f = (ch == '-' ? -1 : f), ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    x *= f;
}
 
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define rrep(m, n, i) for (int i = m; i > n; --i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
#define N 200005
#define fil(a, n) rep(0, n, i) read(a[i])
//---------------https://lunatic.blog.csdn.net/-------------------//
int a;
int b[1005], flag;
vector<pair<int, int>> c;
int main()
{
    int n, m;
    read(n), read(m);
    for (int i = 0; i < n; i++)
    {
        read(a);
        int mo = a % m;
        b[mo]++;
        c.push_back(make_pair(mo, a));
        if (b[mo] >= 2)
            flag = 1;
       // cout << mo << endl;
    }
    if (flag)
    {
        puts("0");
        return 0;
    }
    int ans = 1;
    for (int i = 0; i < c.size(); i++)
    {
        for (int j = i + 1; j < c.size(); j++)
        {
            if (c[i].second > c[j].second)
            {
                ans *= (c[i].first - c[j].first + m);
            }
            else
            {
                ans *= (-c[i].first + c[j].first + m);
            }
            ans %= m;
        }
    }
    cout << ans << endl;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档