https://codeforces.com/problemset/problem/1305/C
题目大意:
给他n个数字a1,a2,...,an。帮助Kuroni计算
1≤i<j≤n | ai - aj |。由于结果可能非常大,请将其输出为m的模数。
如果你不熟悉简短的符号,
1≤i<j≤n|ai-aj|等于|a1-a2|⋅|a1-a3|⋅...⋅|a1-an|⋅|a2-a3|⋅|a2-a4|⋅...⋅|a2-an|⋅...⋅|an1-an|。换句话说,这是所有 1≤i<j≤n 的 |ai-aj| 的乘积。
思路:
注意到模数m很小。
当 n ≤ m时,可以O(n^2)暴力解决;
当 n>m时,由抽屉原理可知必定存在 ai与aj使得 ai ≡ aj (mod m),即∣ai−aj∣modm =0。故此时答案必定为0。
代码实现:
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long LL;
LL a[N];
int n,m;
void solve()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> a[i];
LL ans = 1;
if(n <= m)
{
for(int i = 0; i < n; i ++)
{
for(int j = i+1; j < n; j ++)
{
ans = (ans * abs(a[i] - a[j]) % m) % m;
}
}
cout << ans << endl;
}
else cout << "0" << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(0);
int T = 1;
//cin >> T;
while(T --)
{
solve();
}
return 0;
}