zoukankan      html  css  js  c++  java
  • Kuroni and Impossible Calculation——容斥原理-鸽笼原理-抽屉原理

    题目描述
    已知一个数组a[n],请计算式子:∏_{1≤i<j≤n}|ai−aj| 的值,其中1<=i,j<=n;我们可以认为,这一式子等价于 |a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an|
    输入
    第一行是n,m。第二行是n个整数:a[1],a[2]……a[n]
    输出
    输出 ∏1≤i<j≤n|ai−aj| %m的值
    样例输入
    3 12
    1 4 5
    样例输出
    0
    提示
    数据范围: 2≤n≤2⋅105, 1≤m≤1000,0≤ai≤109

    这道题一看数据范围就知道很亲切,暴力一定是过不了的,但是我还是试了一下手动滑稽
    暴力的结果:黄色的6%
    在这里插入图片描述
    于是乎根据容斥定理想了一下:
    当n>m的时候,可知必定存在 ai 与 aj 使得 ai ≡ aj(mod m)
    换句话说就是| ai - aj|==0此时答案必为零
    当n<=m的情况下,可以直接暴力

    参考代码:

    #include <bits/stdc++.h>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <vector>
    using namespace std;
    #define wuyt main
    typedef long long ll;
    #define HEAP(...) priority_queue<__VA_ARGS__ >
    #define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
    template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
    template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
    ///#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
    ///char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
    ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
    if(c == '-')Nig = -1,c = getchar();
    while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
    return Nig*x;}
    #define read read()
    const ll inf = 1e15;
    const ll INF = 0x3f3f3f3f;
    const int maxn = 2e5 + 7;
    const int mod = 1e9 + 7;
    #define start int wuyt()
    #define end return 0
    ll gcd(ll a,ll b)
    {
        ll t;
        while(b!=0)
        {
            t=a%b;
            a=b;
            b=t;
        }
        return a;
    }
    ll qPow(ll x, ll k)
    {
        ll res = 1;
        while(k) {
            if(k&1)
                res=(res*x);
            k>>=1;
            x=(x*x);
        }
        return res;
    }
    ll maxx=-1;
    ll minn=inf;
    ll num[maxn];
    ll a[maxn];
    ll num2[maxn];
    ll res,ans;
    ll sum;
    map<ll,ll> mp;
    priority_queue <int ,vector<int> ,greater<int> > xiaogen;
    int main()
    {
        ll n=read,m=read;
        ans=1;
        for(int i=1;i<=n;i++) num[i]=read;
        if(n>m){
            printf("0
    ");
            return 0;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++){
                ans*=abs(num[i]-num[j])%m;
                ans%=m;
            }
        }
        cout<<ans%m<<endl;
        return 0;
    }
    
  • 相关阅读:
    [20190415]10g下那些latch是共享的.txt
    [20190415]11g下那些latch是共享的.txt
    [20190409]pre_page_sga=true与连接缓慢的问题.txt
    [20190402]Library Cache mutex.txt
    scrapy简单使用方法
    PHP多进程系列笔(转)
    RdKafka文档翻译
    python判断字符串中是否包含子字符串
    python 逐行读取txt文件
    redis使用watch完成秒杀抢购功能(转)
  • 原文地址:https://www.cnblogs.com/PushyTao/p/13144154.html
Copyright © 2011-2022 走看看