Ozon Tech Challenge 2020 (Div.1 + Div.2)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.

题意:
求出两两差的绝对值的乘积。

思路:

  1. 第一眼2e5的数据认为暴力不可行,以为要推式子,后面想到了鸽巢原理。
  2. 因为m<=1000,所以当n>1000时,答案必定是0。
  3. 为什么?因为n>mod,必定出现a%mod=b%mod -> |a-b|%mod=0
  4. 只要一个部分出现0,那么乘积必定为0 。
  5. 所以n>1000的时候直接输出0,其余的O(n^2)也可以解决。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
using namespace std;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;}void put1(){ puts("Yes") ;}void put2(){ puts("No") ;}
void debug(){printf("T   A   T\n");}void put3(){ puts("-1"); }ll qpf(ll a, ll b, ll p)
{ll ret = 0;while(b){if(b & 1) ret = (ret + a) % p;a = (a + a) % p;b >>= 1;}
return ret % p ;}ll qp(ll a, ll n, ll p){ll ret = 1;while(n){if(n & 1) ret = qpf(ret, a, p);
a = qpf(a, a, p);n >>= 1;}return ret % p ;}//θ=acos(L/2R);
 
const int manx=2e5+5;
 
ll b[manx];
int main(){
    ll n=read(),m=read(),ans=1;
    for(int i=1;i<=n;++i) b[i]=read();
    if(n>1000){
        cout<<0<<endl;
        return 0;
    }
    for(int i=1;i<=n;++i)
        for(int j=i+1;j<=n;++j)
            ans=(ans*abs(b[i]-b[j]))%m;
    cout<<ans%m<<endl;
    return 0;
}
发布了75 篇原创文章 · 获赞 40 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/JiangHxin/article/details/104645890