• C. Kuroni and Impossible Calculation


    To become the king of Codeforces, Kuroni has to solve the following problem.

    He is given nn numbers a1,a2,,ana1,a2,…,an. Help Kuroni to calculate 1i<jn|aiaj|∏1≤i<j≤n|ai−aj|. As result can be very big, output it modulo mm.

    If you are not familiar with short notation, 1i<jn|aiaj|∏1≤i<j≤n|ai−aj| is equal to |a1a2||a1a3||a1−a2|⋅|a1−a3|⋅ … |a1an||a2a3||a2a4|⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … |a2an|⋅|a2−an|⋅ … |an1an|⋅|an−1−an|. In other words, this is the product of |aiaj||ai−aj| for all 1i<jn1≤i<j≤n.

    Input

    The first line contains two integers nn, mm (2n21052≤n≤2⋅105, 1m10001≤m≤1000) — number of numbers and modulo.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai1090≤ai≤109).

    Output

    Output the single number — 1i<jn|aiaj|modm∏1≤i<j≤n|ai−aj|modm.

    Examples
    input
    Copy
    2 10
    8 5
    
    output
    Copy
    3
    input
    Copy
    3 12
    1 4 5
    
    output
    Copy
    0
    input
    Copy
    3 7
    1 4 9
    
    output
    Copy
    1
    Note

    In the first sample, |85|=33mod10|8−5|=3≡3mod10.

    In the second sample, |14||15||45|=341=120mod12|1−4|⋅|1−5|⋅|4−5|=3⋅4⋅1=12≡0mod12.

    In the third sample, |14||19||49|=385=1201mod7|1−4|⋅|1−9|⋅|4−9|=3⋅8⋅5=120≡1mod7.

     

     把n转换到m上去,注意一下模运算与绝对值的关系

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 100007;
    const int N = 1005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    
    int main()
    {
        int n, m;
        cin >> n >> m;
        vector<ll> a(n),b(n);
        vector<bool> vis(N, false);
        for (int i = 0; i < n; i++)
        {
            a[i]=read();
            b[i] = a[i];
            a[i] %= m;
            if (vis[a[i]])
            {
                cout << 0 << endl;
                return 0;
            }
            vis[a[i]] = 1;
        }
        ll ans = 1;
        int tmp = 1;
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if (b[i] < b[j])
                    tmp *= -1;
                ans = ans*(a[i] - a[j]+m) % m;
            }
        }
        cout << (tmp*ans+m)%m << endl;
        return 0;
    }
  • 相关阅读:
    vim命令大全
    docer中运行crontab
    基于预加载的热区域数据的简单设计
    解析Health端点数据获取异常数据
    微服务链路调用耗时示例图
    Spring Cloud health节点通过注册中心扫描状态的简单实现
    转载:Service Mesh:重塑微服务市场--敖小剑
    Springboot统一参数验证方式
    Spirng boot 启动的时候进行监控检查不通过停止服务与自定义健康监控节点
    准备 Python3 和 Python 虚拟环境
  • 原文地址:https://www.cnblogs.com/dealer/p/12777381.html
Copyright © 2020-2023  润新知