Ozon Tech Challenge 2020 C. Kuroni and Impossible Calculation 模数

Posted lasomisolaso~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ozon Tech Challenge 2020 C. Kuroni and Impossible Calculation 模数相关的知识,希望对你有一定的参考价值。

题意:

给出一个数列 a [ ] a[] a[],以及一个模数 m m m,计算 ∏ 1 < = i < j < = n ∣ a [ i ] − a [ j ] ∣ \\prod_1 <= i < j <= n \\left|a[i] - a[j]\\right| 1<=i<j<=na[i]a[j].
数据范围: 1 < = n < = 2 ∗ 1 0 5 , 1 < = m < = 1000 , 0 < = a [ i ] < = 1 0 9 1 <= n <= 2 * 10 ^ 5, 1 <= m <= 1000, 0 <= a[i] <= 10^9 1<=n<=2105,1<=m<=1000,0<=a[i]<=109.

思路:

很容易想到的是 O ( n 2 ) O(n^2) O(n2)的做法,但是看到 n n n的范围,明显会超时。
又会看到 m m m的范围,不是一个固定的非常大的质数当作模数,所以 m m m应该是突破口。
根据 m m m,可以分成两种情况。

  1. n < = m n <= m n<=m, 这样 n n n的范围小于 1000 1000 1000,可以用 O ( n 2 ) O(n ^ 2) O(n2)的算法解决。
  2. n > m n > m n>m,模完 m m m之后最多有 m m m个不同的模数,而数列中的数的个数大于 m m m,一定至少存在两个数, a [ i ] ≡ a [ j ] m o d    m a[i] \\equiv a[j] \\mod m a[i]a[j]modm, 那么 ∣ a [ i ] − a [ j ] ∣ ≡ 0 m o d    m |a[i] - a[j]| \\equiv 0 \\mod m a[i]a[j]0modm.
    最终的乘积就是0.

代码:

/**
* Author : Xiuchen
* Date : 2020-03-03-23.20.52
* Description : C.cpp
*/
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 2e5 + 100;
int gcd(int a, int b)
    return b ? gcd(b, a % b) : a;

ll m, n;
ll a[maxn];
int main()
    scanf("%lld%lld", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    if(n <= m)
        ll ans = 1 % m;
        for(int i = 1; i <= n; i++)
            for(int j = i + 1; j <= n; j++)
                ans = (ans * abs(a[i] - a[j])) % m;
            
        
        printf("%lld\\n", ans);
    
    else printf("0\\n");
    return 0;

以上是关于Ozon Tech Challenge 2020 C. Kuroni and Impossible Calculation 模数的主要内容,如果未能解决你的问题,请参考以下文章

Ozon Tech Challenge 2020 游戏存档

Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)

Ozon Tech Challenge 2020 C. Kuroni and Impossible Calculation 模数

[Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated]-E. Kuroni and the Score Distribution(构造)

Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)

Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)E(构造)