标题标题 Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!) C. Kuroni and Impossible Calculation(同余)
C. Kuroni and Impossible Calculation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
题意 :求数组中任意两数差的绝对值乘积
思路:
观察数据大小,暴力肯定 tle( 一开始想找规律 发现找不出来),重点在取模。
如果其中两个数的差为mod 的倍数 那么结果肯定是0. 也就是说如果 对每个数取模 如果出现了相同的结果 那么肯定是0.
再看 mod 最大是1000,所以取模后的数肯定在0~999,也就是说如果n>1000 肯定出现模相同的情况(只需要n>mod),对于其他情况 暴力即可。
#include<iostream>
#include<cstdio>
#include<map>
#include<math.h>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+7;
typedef long long ll;
ll vis[maxn],a[maxn];
int main (){
ll n,mod,flag=0,ans=1;
scanf("%lld%lld",&n,&mod);
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
vis[a[i]%mod]++;
if(vis[a[i]%mod]>=2) flag=1;
}
if(flag) {
printf ("0\n");
return 0;
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
ans=ans*(abs(a[i]-a[j]))%mod;
}
}
printf ("%lld\n",ans);
}
最后了解一下同余定理…