Kuroni and Impossible Calculation
文章目录
题目描述:
To become the king of Codeforces, Kuroni has to solve the following problem.
He is given nn numbers a1,a2,…,ana1,a2,…,an. Help Kuroni to calculate ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj|. As result can be very big, output it modulo mm.
If you are not familiar with short notation, ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj| is equal to |a1−a2|⋅|a1−a3|⋅|a1−a2|⋅|a1−a3|⋅ …… ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ …… ⋅|a2−an|⋅⋅|a2−an|⋅ …… ⋅|an−1−an|⋅|an−1−an|. In other words, this is the product of |ai−aj||ai−aj| for all 1≤i<j≤n1≤i<j≤n.
Input:
The first line contains two integers nn, mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤10001≤m≤1000) — number of numbers and modulo.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109).
Output:
Output the single number — ∏1≤i<j≤n|ai−aj|modm
Sample Input:
2 10
8 5
Sample Output:
3
Sample Input:
3 12
1 4 5
Sample Output:
0
Sample Input:
3 7
1 4 9
Sample Output:
1
题目描述:
题目先给出所要输入的数字t,以及模(mod)m,要求求出t个数字之间的差绝对值相乘取模(%)。
思路分析:
这道题一看真的很容易,但是做的时候老是在第10个测试点超时,崩溃了,看一下通过人的代码,发现也十分难懂,琢磨了一下,发现找到其中的原理,首先当输入t大于mod的时候,就可以直接以0输出了,这里解释一下原理,在t中(|a_i-a_j|%moda_i%mod-a_j%mod),因为mod的数量只局限于1000,但t大于1000的时候,必然存在a_i%moda_j%mod的情况出现,然后接下来如果小于的话就可以直接按照BF(暴力)解法去搞了
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int b[200200];
int main()
{
int a,mod;
scanf("%d %d",&a,&mod);
for(int i=0;i<a;i++)
{
scanf("%d",&b[i]);
}
if(a>mod){
puts("0");
return 0;
}
int sum=1;
for(int i=0;i<a;i++)
{
for(int j=i+1;j<a;j++)
{
if(abs(b[i]-b[j])%mod==0)
{
printf("0");
return 0;
}
sum=1ll*sum*bs(b[i]-b[j])%mod;//这里1ll是C++中为了将long转为int方便量
}
}
printf("%d",sum);
}