C题 Kuroni and Impossible Calculation

题目

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
Input
2 10
8 5
Output
3
Input
3 12
1 4 5
Output
0
Input
3 7
1 4 9
Output
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.

题意

求解 𝚷1≤ i<j≤ n|ai - aj|%m(m为取模的值)
𝚷1≤ i<j≤ n|ai - aj| =|a1 - a2| * |a1 - a3| |a1 - an|* |a2 - a3| * |a2 - a4| |a2 - an|*… * |an-1 - an|. 言外之意,求解 |ai - aj| 乘积。(1 ≤ i < j ≤ n)

思路

根据雀巢定理可得,当n>m的时候总有两个数%m 相等,根据同余定理得,他俩的差%m等于0
m最大是1000,所以n>m 的时候或者n>1000的时候答案就是0
剩下的就1000 暴力写就完事

代码

#include <cmath>
#include <iostream>
typedef long long ll;
using namespace std;
const int maxn = 200000+100;
int a[maxn];
int main()
{
	ios::sync_with_stdio(0);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	if(n>=m+1) cout<<"0\n";
	else
	{
		ll ans = 1;
		for(int i=1;i<=n-1;i++)
		for(int j=i+1;j<=n;j++)
			ans = ans * abs(a[i] - a[j]) % m;
		cout<<ans % m<<"\n";
	}
        return 0;
} 
/*
n>m的时候,根据抽屉定理,总会有两个数字mod m相等,因为如果前m个都没
重复的话, 第m+1个肯定重复了 
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值