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 a 1 , a 2 , … , a n a1,a2,…,an a1,a2,,an. Help Kuroni to calculate ∏ 1 ≤ i < j ≤ n   ∣ a i − a j ∣ ∏1≤i<j≤n\ |ai−aj| 1i<jn aiaj. As result can be very big, output it modulo m.

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

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

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

Output
Output the single number — ∏ 1 ≤ i < j ≤ n   ∣ a i − a j ∣   m o d   m ∏1≤i<j≤n\ |ai−aj|\ mod\ m 1i<jn aiaj 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 < = 2 e 5 ) n (n<=2e5) n(n<=2e5) 个非负整数的序列,以及正整数 m ( m < = 1000 ) m (m<=1000) m(m<=1000),求 ∏ 1 ≤ i < j ≤ n   ∣ a i − a j ∣   m o d   m ∏1≤i<j≤n\ |ai−aj|\ mod\ m 1i<jn aiaj mod m 的值。

思路: 由抽屉原理可知,当 n > m n>m n>m 时,必定存在两个 a i   m o d   m ai\ mod\ m ai 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值