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;
}
Wannafly Camp 2020 Day 1A 期望逆序对 - 概率期望的更多相关文章
- 计数 luogu 4223 期望逆序对
https://www.luogu.org/problemnew/show/P4223 期望乘以\(\binom {n}{2}^k\)变成了计数问题 我们考虑每一组数\((A, B)\)产生的贡献CC ...
- BZOJ5058 期望逆序对 【矩乘 + 组合数学 + 树状数组】
题目链接 BZOJ5058 题解 可以发现任意两个位置\(A,B\)最终位置关系的概率是相等的 如果数列是这样: CCCCACCCCBCCCC 那么最终有\(7\)种位置关系 \((A,B)\) \( ...
- Wannafly Camp 2020 Day 3I N门问题 - 概率论,扩展中国剩余定理
有一个猜奖者和一个主持人,一共有 \(n\) 扇门,只有一扇门后面有奖,主持人事先知道哪扇门后有奖,而猜奖者不知道.每一轮,猜奖者选择它认为的有奖概率最大(如果有多个最大,随机选一个)的一扇门,主持人 ...
- Wannafly Camp 2020 Day 3F 社团管理 - 决策单调性dp,整体二分
有 \(n\) 个数构成的序列 \({a_i}\),要将它划分为 \(k\) 段,定义每一段的权值为这段中 \((i,j) \ s.t. \ i<j,\ a_i=a_j\) 的个数,求一种划分方 ...
- Wannafly Camp 2020 Day 3D 求和 - 莫比乌斯反演,整除分块,STL,杜教筛
杜教筛求 \(\phi(n)\), \[ S(n)=n(n+1)/2-\sum_{d=2}^n S(\frac{n}{d}) \] 答案为 \[ \sum_{d=1}^n \phi(d) h(\fra ...
- Wannafly Camp 2020 Day 2B 萨博的方程式 - 数位dp
给定 \(n\) 个数 \(m_i\),求 \((x_1,x_2,...,x_n)\) 的个数,使得 \(x_1 \ xor\ x_2\ xor\ ...\ xor\ x_n = k\),且 \(0 ...
- Wannafly Camp 2020 Day 2D 卡拉巴什的字符串 - 后缀自动机
动态维护任意两个后缀的lcp集合的mex,支持在串末尾追加字符. Solution 考虑在 SAM 上求两个后缀的 LCP 的过程,无非就是找它们在 fail 树上的 LCA,那么 LCP 长度就是这 ...
- Wannafly Camp 2020 Day 1D 生成树 - 矩阵树定理,高斯消元
给出两幅 \(n(\leq 400)\) 个点的无向图 \(G_1 ,G_2\),对于 \(G_1\) 的每一颗生成树,它的权值定义为有多少条边在 \(G_2\) 中出现.求 \(G_1\) 所有生成 ...
- Wannafly Camp 2020 Day 2I 堡堡的宝藏 - 费用流
感谢这道题告诉我KM求的是 完备 最大权匹配 :( #include <bits/stdc++.h> using namespace std; #define reset(x) memse ...
随机推荐
- nginx 正向代理与反向代理
一.介绍 反向代理:让Internet上的用户可以访问局域网内的资源,中间设置一个代理服务器,如下所示,红色圈是指局域网内的站点(myweb站点是我们的站点,例如iis).箭头不能反过来 正向代理:客 ...
- Linux系统开机显示BusyBox v1.22.1 built-in shell(ash) 解决方法
BusyBox 是一个集成了三百多个最常用Linux命令和工具的软件.BusyBox 包含了一些简单的工具,例如ls.cat和echo等等,还包含了一些更大.更复杂的工具,例grep.find.mou ...
- vim编辑超大文件
进入大文件(12g,250w+ lines),vim,耐心等待 有两种方法编辑删除冗余字段 1.set number ,可以通过:+数字组合跳到指定行,输入命令 ":100,200d&q ...
- Gird(2)
目录 grid 布局(2) grid区域属性 网格线名称 grid-template-areas 属性 grid-auto-flow 容器内子元素的属性 grid 布局(2) grid区域属性 网格线 ...
- Linux ps和pstree命令
1. 查看所有进程 ps -eF -e: Select all processes.-F: Extra full format. PSR (Processor)显示进程所在的CPU. 2. 查看所有进 ...
- setter&getter
let _age = 4 class Animal { construct (type){ this.type = type } get age(){ return _age } set age(va ...
- SpringBoot从1.5.1→2.2.4项目加包扫雷一:Error:(8, 44) java: 程序包org.springframework.boot.web.support不存在
更换成新包即可import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
- PHP0002:PHP基础1
PHP基础 一个表单同时提交get 和 post php保存数据到文件
- .net core 简单定时程序
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Orleans; using S ...
- python正式学习第二天
用python操作文件 步骤一:找到文件,打开文件 步骤二:修改文件 ,读取文件 步骤三:关闭文件,并保存 用代码演示如下: 1. f = open(file nama)2. f.read(#读取的字 ...