C. Kuroni and Impossible Calculation
The meaning of problems
A given number of columns \ (A [] \) , and a modulus \ (m \) , calculated \ ({Π_. 1 <= I <J <= n-} \) \ (|a [I] -a [ j] | \)
数据范围:\(1<=n<=2∗10^5,1<=m<=1000,0<=a[i]<=10^9\)
Thinking
It is easy to think of (O (n ^ 2) \ ) \ practice, but to see the range of n, obviously time out.
M ranges will be seen, is not a very large number of fixed mass as modulus, it should be a breakthrough m.
The m, can be divided into two cases.- \ (n <= m \) , n is such a range less than 1000, can be \ (O (n ^ 2) \) solution
- \ (n-> m \) , m has finished after molding up to m different modulus, and the counted number is greater than the number of columns m, there must be at least two numbers, \ (A [I] ≡a [J] MODM \) , then the \ (|a [I] -a [J] |≡0modm \) .
the final product is 0.
Code
#include<bits/stdc++.h> using namespace std; const int maxn = 2e5+10; typedef long long ll; ll a[maxn]; int book[1010]; int main(void){ int n,m; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); if(book[a[i]%m]++){ printf("0\n"); return 0; } } ll ans = 1; for(int i = 1; i <= n; i++){ for(int j = i+1; j <= n; j++){ ans *= abs(a[i]-a[j]); ans %= m; if(ans==0){ printf("%lld\n",ans); return 0; } } } printf("%lld\n",ans); return 0; }