Hellc
【题目描述】
作为一个生活散漫的人,小 Z 每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小 Z 再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
具体来说,小 Z 把这 $N$ 只袜子从 $1$ 到 $N$ 编号,然后从编号 $L$ 到 $R$ 的袜子中随机选取,尽管小 Z 并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
你的任务便是告诉小 Z ,他有多大的概率抽到两只颜色相同的袜子。当然,小 Z 希望这个概率尽量高,所以他可能会询问多个 $(L, R)$ 以方便自己选择。
【题目链接】
BZOJ 2038 小 Z 的袜子 【国家集训队 2009】
【解题思路】
在区间 $[l, r]$ 内,设 $S$ 表示袜子的颜色集合,$f(x)$ 表示颜色 $x$ 出现的次数,根据古典概型:
$$
ans = frac {sum_{x in S} C(2, f(x))} {C(2, r - l + 1)}
$$
分母可以直接求出。
考虑到 $C(x, 2) = x(x - 1) = x^2 - x$,不妨将分子展开:
$$
begin{align*}
sum_{x in S} C(2, f(x))
& = sum_{x in S} (f^2(x) - f(x)) \
& = sum_{x in S} f^2(x) - sum_{x in S} f(x) \
& = sum_{x in S} f^2(x) - (r - l + 1)
end{align*}
$$
所以我们需要求的是每种颜色出现次数的平方和。
这可以用莫队算法解决,详见 莫队算法 - 学习笔记。
【AC代码】
#include <cstdio>
#include <algorithm>
#include <cmath>
typedef long long int64;
inline int64 sqr(int64 x){
return x * x;
}
inline int64 gcd(int64 a, int64 b){
int64 d = 1;
while(a && b){
while(~a & 1 && ~b & 1) a >>= 1, b >>= 1, d <<= 1;
while(~a & 1) a >>= 1;
while(~b & 1) b >>= 1;
if(a < b) std::swap(a, b);
a = a - b >> 1;
}
return std::max(a, b) * d;
}
inline void reduce(int64 &u, int64 &d){
int64 g = gcd(u, d);
u /= g, d /= g;
}
const int MAXN = 50000;
const int MAXM = 50000;
int n, m;
int a[MAXN];
int64 ansU[MAXM], ansD[MAXM];
int blockSize;
struct Query{
int l, r;
int id;
inline friend bool operator<(const Query &a, const Query &b){
if(a.l / blockSize != b.l / blockSize) return a.l / blockSize < b.l / blockSize;
else return a.r < b.r;
}
void calc(int64 sqrSum){
ansU[id] = sqrSum - (r - l + 1);
ansD[id] = (int64大专栏 Hellc>)(r - l) * (r - l + 1);
reduce(ansU[id], ansD[id]);
}
} querys[MAXM];
int l, r;
int f[MAXN + 1];
bool in[MAXN];
int64 currAns;
inline void flip(int pos){
in[pos] ^= 1;
currAns -= sqr(f[ a[pos] ]);
if(in[pos]){
f[ a[pos] ]++;
} else{
f[ a[pos] ]--;
}
currAns += sqr(f[ a[pos] ]);
}
inline void solve(){
blockSize = static_cast<int>(std::ceil(std::sqrt(n)) + 1e-6);
std::sort(querys, querys + m);
l = 0, r = 0, flip(0);
for(Query *q = querys; q != querys + m; q++){
while(l > q->l) flip(--l);
while(r < q->r) flip(++r);
while(l < q->l) flip(l++);
while(r > q->r) flip(r--);
q->calc(currAns);
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
for(int i = 0; i < m; i++){
Query *q = &querys[i];
scanf("%d%d", &q->l, &q->r), q->l--, q->r--;
q->id = i;
}
solve();
for(int i = 0; i < m; i++) printf("%lld/%lldn", ansU[i], ansD[i]);
return 0;
}
就是这样咯~
Hellc的更多相关文章
- JavaWeb 11_jsp九大内置对象
1. out: 输出对象,向客户端输出内容2. request: 请求对象;存储"客户端向服务端发送的请求信息" request对象的常见方法: String getParamet ...
随机推荐
- MySQL导入sql文件,过大导致错误
--导入sql脚本文件,报错: Navicat 导入数据报错 --- 1153 - Got a packet bigger than 'max_allowed_packet' bytes2006 - ...
- nfs存储(一)
排错思路: .环境问题 SElinux firewalld 网络是否能通 .配置问题 所有的故障只会出现在你曾经修改过的文件中 /etc/rsyncd.conf /etc/rsyncd.passwor ...
- [ZJOI2019]语言(树链剖分+动态开点线段树+启发式合并)
首先,对于从每个点出发的路径,答案一定是过这个点的路径所覆盖的点数.然后可以做树上差分,对每个点记录路径产生总贡献,然后做一个树剖维护,对每个点维护一个动态开点线段树.最后再从根节点开始做一遍dfs, ...
- 对象数组和for循环遍历输出学生的信息
public class Student { private int no; private String name; private int age; public Student(int no, ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- 52)PHP,加了单例模式的数据库代码
<?php class db { public $host ;//= "localhost";//定义默认连接方式 public $User;//= "root&q ...
- 浅谈PHP小马免杀
在渗透测试过程初期,上传小马,拿到 webshell 再进行下一步的操作,现如今的网站安全更多是 一些云防护.CDN防护.服务器安全软件等等,给渗透测试.提权等带来了一定难度的提升, 今天探讨一下如何 ...
- hdu2876 Connections between cities(LCA倍增)
图不一定联通,所以用并查集找各个联通块的祖先分别建图,之后就和LCA的步骤差不多了 #include<iostream> #include<cstring> #include& ...
- 第二类错误|检验统计量|左偏|右偏|P值
6 第二类错误在H0中的假设值差别越大时增大? 不对,第二类错误在H0中的假设值差别越大时变小. 检验统计量有哪些? 根据假设内容确定是左偏还是右偏? P值是在原假设为真的条件下,检验统计量大于或等于 ...
- 基于 maven 的ssm 框架搭建
1.新建一个 maven 工程, war 包 2.引入 pom 文件(springmvc+spring+mybatis) 3.引入配置文件 4.引入页面,编写 contorller 层测试 5.编写查 ...