Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 离散化】
任意门:http://codeforces.com/contest/689/problem/E
E. Mike and Geometry Problem
3 seconds
256 megabytes
standard input
standard output
Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to be the number of integer points in the segment [l, r] with l ≤ r (say that ). You are given two integers nand k and n closed intervals [li, ri] on OX axis and you have to find:
In other words, you should find the sum of the number of integer points in the intersection of any k of the segments.
As the answer may be very large, output it modulo 1000000007 (109 + 7).
Mike can't solve this problem so he needs your help. You will help him, won't you?
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of segments and the number of segments in intersection groups respectively.
Then n lines follow, the i-th line contains two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109), describing i-th segment bounds.
Print one integer number — the answer to Mike's problem modulo 1000000007 (109 + 7) in the only line.
3 21 21 32 3
5
3 31 31 31 3
3
3 11 22 33 4
6
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
大概题意:
有 N 个区间, 从其中取 K 个区间。所以有 C(N, K)种组合, 求每种组合区间交集长度的总和。
解题思路:
丢开区间的角度,从每个结点的角度来看,其实每个结点的贡献是 C(cnt, K) cnt 为该结点出现的次数, 所以只要O(N)扫一遍统计每个结点的贡献就是答案。
思路清晰,但考虑到数据的规模,这里需要注意和需要用到两个技巧:
一是离散化,这里STL里的 vector 和 pair 结合用,结合区间加法的思想进行离散化。
二是求组合数时 除数太大,考虑到精度问题需要用逆元来计算。
AC code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2e5+7; 4 const int mod = 1e9+7; 5 long long fac[maxn]; 6 7 long long qpow(long long a,long long b) //快速幂 8 { 9 long long ans=1;a%=mod; 10 for(long long i=b;i;i>>=1,a=a*a%mod) 11 if(i&1)ans=ans*a%mod; 12 return ans; 13 } 14 15 long long C(long long n,long long m) //计算组合数 16 { 17 if(m>n||m<0)return 0; 18 long long s1=fac[n], s2=fac[n-m]*fac[m]%mod; //除数太大,逆元处理 19 return s1*qpow(s2,mod-2)%mod; 20 } 21 int n,k; 22 int l[maxn],r[maxn]; //左端点, 右端点 23 int main() 24 { 25 fac[0]=1; 26 for(int i=1;i<maxn;i++) //预处理全排列 27 fac[i]=fac[i-1]*i%mod; 28 29 scanf("%d%d",&n,&k); 30 for(int i=1;i<=n;i++){ 31 scanf("%d",&l[i]); 32 scanf("%d",&r[i]); 33 } 34 vector<pair<int,int> >op; 35 for(int i=1;i<=n;i++){ //离散化 36 op.push_back(make_pair(l[i]-1,1)); //区间加法标记 37 op.push_back(make_pair(r[i],-1)); 38 } 39 sort(op.begin(),op.end()); //升序排序 40 long long ans = 0; //初始化 41 int cnt=0; 42 int la=-2e9; 43 for(int i=0;i<op.size();i++){ //计算每点的贡献 44 ans=(ans+C(cnt,k)*(op[i].first-la))%mod; 45 la=op[i].first; 46 cnt+=op[i].second; //该点的前缀和就是该点的出现次数 47 } 48 cout<<ans<<endl; 49 }
View Code
转载于:https://www.cnblogs.com/ymzjj/p/9629418.html
Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 离散化】相关推荐
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs
B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...
- Codeforces Round #410 (Div. 2) D. Mike and distribution 思维+数学
链接: http://codeforces.com/contest/798/problem/D 题意: 给你两个长度为n的数列a和b,让你选n/2+1个下标,使得2*∑ai>suma,2*∑bi ...
- Codeforces Round #305 (Div. 1) D. Mike and Fish 欧拉回路
传送门 文章目录 题意: 思路: 题意: 思路: 欧拉回路经典题. 将其转换成图上问题,对于横纵坐标我们将其分开,对于(x,y)(x,y)(x,y)我们将其横纵坐标连一个无向边,现在问题就转换成了我们 ...
- Codeforces Round #361 (Div. 2) C 二分
链接:戳这里 C. Mike and Chocolate Thieves time limit per test2 seconds memory limit per test256 megabytes ...
- Codeforces Round #410 (Div. 2) D. Mike and distribution(贪心)
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- Codeforces Round #361(div 2)
A题题目意思很简单,问一种拨号的方式(拨号手势)是不是能拨出唯一的号码(例如253就不是唯一的,因为586也是可以的) 记录电话上每个格子上下左右是否还有格子,一个拨号手势是唯一的当且仅当,所拨号码的 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)
题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...
- Codeforces Round #441 Div. 2题解
A.直接判断相邻的边是不是最短边 是就来回走 不然就走一条第二的然后再来回走 #include<cstdio> #include<cstring> #include<al ...
- Codeforces Round #706 (Div. 2)-B. Max and Mex-题解
目录 Codeforces Round #706 (Div. 2)-B. Max and Mex Problem Description Input Output Sample Input Sampl ...
最新文章
- 《基于张量网络的机器学习入门》学习笔记1
- 博通1300亿美元收购高通,一场充满大饼和落井下石的“大戏”
- Twitch 沈悦时:国内外互联网直播生态差异
- 三角剖分求多边形面积的交 HDU3060
- 游族网络:中诚信国际将公司主体及游族转债列入信用评级观察名单
- 关于vc++调用 exe文件的问题
- 车轮轴承行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
- matlab随机数的生成,MATLAB随机数生成器
- python爬取千图网_scrapy之千图网全站爬虫
- Python分析【崩坏学园2】凝魔纹心所输出
- FPGA基础之cyclone_iv资源概述
- 阿里云图片如何获取缩略图
- Vue单元素/组件的过渡
- 安警官的IP地址是怎样定位到莽村附近的?
- 电化学线性极化曲线的Tafel外推法(Tafel拟合)得到年腐蚀速率和极化电阻的原理(科研投稿)
- 什么是PEP8编码风格?
- linux 内核list head,Linux内核之list_head.pdf
- 基于Vue+Java实现的在线聊天APP系统设计与实现
- 响铃:爆雷=靴子落地,P2P或不再负重前行
- ubuntu18.04安装openvino2022.1