• Wannafly Camp 2020 Day 1A 期望逆序对


    分类讨论即可

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N = 5005;
    const int mod = 998244353;
    int i2;
    int qpow(int p,int q) {
        int b=p,r=1;
        while(q>0) {
            if(q&1) r*=b;
            b*=b;
            r%=mod;
            b%=mod;
            q>>=1;
        }
        return r;
    }
    int inv(int x) {
        return qpow(x,mod-2);
    }
    
    struct seg {
        int l,r,inv;
        int len() {
            return r-l+1;
        }
        seg operator + (const seg &b) {
            seg c;
            c.l = min(l,b.l);
            c.r = max(r,b.r);
            return c;
        }
        seg operator * (const seg &b) {
            seg c;
            c.l = max(l,b.l);
            c.r = min(r,b.r);
            return c;
        }
        bool operator < (const seg &b) {
            return (l+r) < (b.l+b.r);
        }
    } a[N];
    
    
    struct num {
        int x;
        num() {
            x=0;
        }
        num(int t) {
            x=t;
        }
        num operator + (const num &b) {
            num c;
            c.x = x + b.x;
            c.x %= mod;
            return c;
        }
        num operator - (const num &b) {
            num c;
            c.x = x - b.x;
            c.x %= mod;
            c.x += mod;
            c.x %= mod;
            return c;
        }
        num operator * (const num &b) {
            num c;
            c.x = x * b.x;
            c.x %= mod;
            return c;
        }
        num operator / (const num &b) {
            num c;
            if(b.x==2)
                c.x = x * i2;
            else
                c.x = x * inv(b.x);
            c.x %= mod;
            return c;
        }
        num operator / (const seg &b) {
            num c;
            c.x = x * b.inv;
            c.x %= mod;
            return c;
        }
        void operator = (const int &b) {
            x = b;
        }
        void operator = (const num &b) {
            x = b.x;
        }
        num operator + (const int &b) {
            num c;
            c.x = x + b;
            c.x %= mod;
            return c;
        }
        num operator - (const int &b) {
            num c;
            c.x = x - b;
            c.x %= mod;
            return c;
        }
        num operator * (const int &b) {
            num c;
            c.x = x * b;
            c.x %= mod;
            return c;
        }
        num operator / (const int &b) {
            num c;
            c.x = x * inv(b);
            c.x %= mod;
            return c;
        }
        int get() {
            return x;
        }
    };
    
    
    int n;
    
    num calc(seg a,seg b) {
        if(a.r<b.l) return num(0);
        if(b.r<a.l) return num(1);
        if(a.l>=b.l && a.r<=b.r) { //cout<<"A";
            return (num(a.l-b.l)+num(a.r-a.l)/num(2))/b;
        }
        if(b.l>=a.l && b.r<=a.r) { //cout<<"B";
            return (num(a.r-b.r)+num(b.r-b.l)/num(2))/a;
        }
        if(a.l>=b.l && b.r<=a.r) { //cout<<"C";
            return num(1)-num(b.r-a.l+1)*num(b.r-a.l+2)/num(2)/b/a;
        }
        if(b.l>=a.l && a.r<=b.r) { //cout<<"D";
            return num(a.r-b.l)*num(a.r-b.l+1)/num(2)/b/a;
        }
    }
    
    num check(seg a,seg b) {
        num x;
        for(int i=a.l;i<=a.r;i++) {
            for(int j=b.l;j<=b.r;j++) {
                if(j<i) x=x+1;
            }
        }
        x=x/((a.r-a.l+1)*(b.r-b.l+1));
        return x;
    }
    
    signed main() {
        i2=inv(2);
        scanf("%lld",&n);
        //for(int i=1;i<=n;i++) arrinv[i]=__inv(i);
        for(int i=1;i<=n;i++) {
            int t1,t2;
            scanf("%lld%lld",&t1,&t2);
            a[i].l=t1;
            a[i].r=t2;
            a[i].inv=inv(a[i].r-a[i].l+1);
        }
        sort(a+1,a+n+1);
        num ans = 0;
        /*for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                cout<<(calc(a[i],a[j])).get()<<" ";
            }
            cout<<endl;
        }
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                cout<<(check(a[i],a[j])).get()<<" ";
            }
            cout<<endl;
        }*/
    
        for(int i=2;i<=n;i++) {
            for(int j=1;j<i;j++) {
                ans = ans + calc(a[j],a[i]);
            }
        }
        cout<<ans.get()<<endl;
    }
    
  • 相关阅读:
    Linux下JDK安装位置
    Oracle中的User与Schema
    MFC中不同窗口间的切换
    MFC 动态的画线 画当前的线是清除上一次画的线
    VC ADO连接ACCESS步骤及错误处理
    虚继承和虚函数继承
    虚拟机无法连接连接U盘
    未能找到类型或命名空间
    VS2008 重构 封装字段 出错 无法使用
    vs2010 Visual Studio 遇到了异常。这可能是由某个扩展导致的
  • 原文地址:https://www.cnblogs.com/mollnn/p/12254263.html
Copyright © 2020-2023  润新知