Ozon Tech Challenge 2020 (Div.1 + Div.2) C. Kuroni and Impossible Calcul 鹊巢原理

传送门

文章目录

题意:

给你一个数组 a n a_n an​,求 ∏ 1 ≤ i < j ≤ n ∣ a j − a i ∣   m o d   m \begin{matrix} \prod_{1\le i<j\le n} |a_j-a_i| \end{matrix}\bmod m ∏1≤i<j≤n​∣aj​−ai​∣​modm。
n ≤ 2 e 5 , m ≤ 1 e 3 n\le2e5,m\le 1e3 n≤2e5,m≤1e3

思路:

一开始想推公式,发现不是很好推,怎么样都避免不了 n 2 n^2 n2遍历,所以考虑别的办法。
注意到 m ≤ 1 e 3 m\le1e3 m≤1e3,所以可以从 m m m入手。
由鹊巢原理可知,当 n > m n>m n>m的时候,一定存在两个数 a i , a j a_i,a_j ai​,aj​,他们 ( a i − a j )   m o d   m = 0 (a_i-a_j)\bmod m=0 (ai​−aj​)modm=0,所以对于 n > m n>m n>m的情况直接输出 0 0 0,否则 n 2 n^2 n2暴力跑即可。

// Problem: C. Kuroni and Impossible Calculation
// Contest: Codeforces - Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)
// URL: https://codeforces.com/contest/1305/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,mod;
int a[N];

LL qmi(LL a,LL b) {
	LL ans=1;
	a%=mod;
	while(b) {
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	scanf("%d%d",&n,&mod);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	if(n>mod) {
		puts("0");
		return 0;
	}
	LL ans=1;
	for(int i=1;i<=n;i++) {
		for(int j=1;j<i;j++) 
			(ans*=abs(a[i]-a[j])%mod)%=mod;
	}
	printf("%lld\n",ans);




	return 0;
}
/*

*/


上一篇:Ozon Tech Challenge 2020 (Div.1 + Div.2) F. Kuroni and the Punishment 随机化


下一篇:08:Challenge 1