Ozon Tech Challenge 2020 (Div.1 + Div.2) C. Kuroni and Impossible Calculation(抽屜原理)

原題鏈接:https://codeforces.com/contest/1305/problem/C


To become the king of Codeforces, Kuroni has to solve the following problem.

He is given n numbers a1,a2,,ana1,a2,…,an. Help Kuroni to calculate 1i<jn aiaj∏1≤i<j≤n\ |ai−aj|. As result can be very big, output it modulo m.

If you are not familiar with short notation, 1i<jn aiaj∏1≤i<j≤n\ |ai−aj| is equal to a1a2a1a3a1ana2a3a2a4a2anan1an|a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an|. In other words, this is the product of aiaj|ai−aj| for all 1i<jn1≤i<j≤n.

Input
The first line contains two integers n,m(2n2105,1m1000)n, m (2≤n≤2⋅105, 1≤m≤1000) — number of numbers and modulo.

The second line contains n integers a1,a2,,an(0ai109)a1,a2,…,an (0≤ai≤109).

Output
Output the single number — 1i<jn aiaj mod m∏1≤i<j≤n\ |ai−aj|\ mod\ m.

輸入1:

2 10
8 5

輸出1:

3

輸入2:

3 12
1 4 5

輸出2:

0

輸入3:

3 7
1 4 9

輸出3:

1

題意: 給一個 n(n<=2e5)n (n<=2e5) 個非負整數的序列,以及正整數m(m<=1000)m (m<=1000),求 1i<jn aiaj mod m∏1≤i<j≤n\ |ai−aj|\ mod\ m 的值。

思路: 由抽屜原理可知,當 n>mn>m 時,必定存在兩個 ai mod mai\ mod\ m同餘,此時答案爲 0. 否則 n 小於等於 1000,可以直接暴力。

Code:

#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=2e5+100;
ll a[N],ans=1;
int main(){
    ll n,m;    cin>>n>>m;
    for(int i=0;i<n;i++)
        cin>>a[i];
    if(n>m) return cout<<0<<endl,0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            ans=ans*(ll)abs(a[j]-a[i])%m;
    cout<<ans<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章