思维 + 暴力 CodeForces - 1305C

Problem Description

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.

很失败,思考方向走错了,心里想着怎么化简来着,明明做过很多数论的题,这道还是想错了,以后遇到 mod 的题要先考虑是不是为零的情况。加油啊,你的发量经不起这么耗了,拿出点 实力来啊!!

#include <iostream>
#include <cmath>

using namespace std;

typedef long long LL;

const int N = 300100;

int a[N];


int main()
{
    int n, m; cin >> n >> m;
    
    for(int i = 1; i <= n ; i ++)  scanf("%d", a + i);
    
    if(n > m) // 若 n > m,则肯定会存在两个 %m 相同的数 a1 和 a2,即 (a1 - a2) % m == 0,得出结果肯定为0
    {
        cout << 0 << endl;
        return 0;
    }
    else 
    {
        LL res = 1;
        for(int i = 1; i <= n ; i ++)
        {
            for(int j = i + 1; j <= n; j ++)
            {
                res = res * abs(a[i] - a[j]) % m;
            }
        }
        cout << res << endl;
    }
    return 0;
}
发布了53 篇原创文章 · 获赞 14 · 访问量 1901

猜你喜欢

转载自blog.csdn.net/weixin_45630535/article/details/104757410
今日推荐