【题目描述】

作为一个生活散漫的人,小 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的更多相关文章

  1. JavaWeb 11_jsp九大内置对象

    1. out: 输出对象,向客户端输出内容2. request: 请求对象;存储"客户端向服务端发送的请求信息" request对象的常见方法: String getParamet ...

随机推荐

  1. split - 拆分文件

    拆分文件 # 每个文件的行数为1000行 split -l 1000 test.txt # 将test文件拆分,20M一个文件 split -b 20M test.txt test文件拆分,并且文件名 ...

  2. Educational Codeforces Round 80 (Rated for Div. 2)D E

    D枚举子集 题:https://codeforces.com/contest/1288/problem/D题意:给定n个序列,每个序列m个数,求第i个和第j个序列组成b序列,b序列=max(a[i][ ...

  3. UML- 其他需求制品有哪些?

     1.其他需求 补充性规格说明(非功能性需求):性能/稳定性.文档.报表.许可授权等. 词汇表 设想:执行摘要. 业务规则(领域规则):如税法 2.准则 初始阶段无需对其他需求彻底分析.但花费一定时间 ...

  4. 使用OkHttp上传图片到服务器

    Okhttp上传图片方法,就像网页那样,使用Form的Post. 首先创建requestBody,然后Builder构建Query:最后Response返回服务器请求,最后把response.body ...

  5. ionic 打包时所遇问题记录

    问题1 ----------------------- Error occurred during initialization of VM Could not reserve enough spac ...

  6. springboot系列教程导学篇

    spring boot2.0系列教程学习之导学篇 springboot 2.0深度学习系列教程. Spring Boot 虽然凯哥从2015年年初开始就接触了spring boot.但是在之后的公司中 ...

  7. django框架基础-ORM跨表操作-长期维护

    ###############    一对一跨表查询    ################ import os if __name__ == '__main__': os.environ.setde ...

  8. freeRadius日志关闭

    vim /etc/raddb/radiusd.conf #file = ${logdir}/radius.log file = /dev/null vim /etc/raddb/modules/det ...

  9. python心得二(编码问题)

    内容编码 字码发展1.ascii(只识别英文)8位就可以表示所有英文,字符数字,1个字节就可以 2.unicode(万国码)最少两个字节中文三个字节 3.utf-8万国码存在空间浪费英文8位中文24位 ...

  10. Linux 下centos7启动 Tomcat 抛出Can't connect to X11 window server 问题的解决方法

    1 问题 今天启动 Tomcat 后,登录页验证码不见了.在 localhost.xxx.log 发现以下错误: org.apache.catalina.core.StandardWrapperVal ...