codeforces 361 E - Mike and Geometry Problem
原题:
Description
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 n and 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?
Input
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.
Output
Print one integer number — the answer to Mike's problem modulo 1000000007 (109 + 7) in the only line.
Sample Input
3 2
1 2
1 3
2 3
5
3 3
1 3
1 3
1 3
3
3 1
1 2
2 3
3 4
6
Hint
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
提示:数据比较大,需要离散化。可以一段段考虑。
数轴上一段被选取的次数就和几个集合包含它有关了,算组合数。可以直接做出覆盖次数。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 2e5+;
const int MOD = 1e9+; int l[N], r[N];
int sum[N*];
int disc[N*];
int fac[N];
int n, k; LL qpow(LL x, LL k)
{
LL res = ;
while(k)
{
if(k & ) res = res * x % MOD;
x = x * x % MOD;
k >>= ;
}
return res;
} LL inv(LL a, LL n)
{
return qpow(a, n - );
} int C(int n, int m)
{
if (n < m) return ;
return (LL)fac[n] * inv(fac[m], MOD) % MOD * inv(fac[n-m], MOD) % MOD;
} int main()
{
//预处理阶乘数
fac[] = ;
for (int i = ; i < N; i++)
{
fac[i] = ((LL)fac[i-] * i) % MOD;
} while (scanf("%d%d", &n, &k) == )
{
int tot = ;
for (int i = ; i < n; i++)
{
scanf("%d%d", l+i, r+i);
disc[tot++] = l[i];
disc[tot++] = ++r[i];
}
sort(disc, disc + tot);
tot = unique(disc, disc + tot) - disc; for (int i = ; i < n; i++)
{
int l = lower_bound(disc, disc + tot, ::l[i]) - disc;
int r = lower_bound(disc, disc + tot, ::r[i]) - disc;
sum[l]++;
sum[r]--;
} int ans = ;
for (int i = ; i < tot; i++)
{
sum[i] += sum[i-];
ans = (ans + (LL)C(sum[i-], k) * (disc[i] - disc[i-])) % MOD;
}
printf("%d\n", ans);
memset(sum, , sizeof sum);
}
return ;
}
codeforces 361 E - Mike and Geometry Problem的更多相关文章
- codeforces 689E E. Mike and Geometry Problem(组合数学)
题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...
- codeforces 689 E. Mike and Geometry Problem 组合数学 优先队列
给定一个函数: f([l,r]) = r - l + 1; f(空集) = 0; 即f函数表示闭区间[l,r]的整点的个数 现在给出n个闭区间,和一个数k 从n个区间里面拿出k个区间,然后对这k个区间 ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...
- CodeForces 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- CodeForces 689E Mike and Geometry Problem
离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- sed详细分析
[一.简单描述] sed命令类似命令行的文本编辑器,以行为单位(见注1).除非带命令i(in-place)否则源文件内容并不会被更新. [二.使用] [2.1.使用方式] 存在两种使用方式: 1. ...
- 深入浅出数据分析 Head First Data Analysis Code 数据与代码
<深入浅出数据分析>英文名为Head First Data Analysis Code, 这本书中提供了学习使用的数据和程序,原书链接由于某些原因不 能打开,这里在提供一个下载的链接.去下 ...
- 2009年到2013年甲子园ED
2009年 2010年 最喜欢的一个!看过N遍 2011年 也不错! 2012年 超级好听啊~^_^比10年的还好,看过N+1遍……o(╯□╰)o 2013年春季甲子园 2013年夏季 印象最深的 ...
- eclipse中运行项目时报Class not found的错误
环境:Groovy/Grails Tool Suite 3.1.0.RELEASE(BASED ON ECLIPSE JUNO 3.8.1).JDK1.6 运行class的main方法,或启动juni ...
- Codeforces Round #157 (Div. 2)
A. Little Elephant and Chess 模拟. B. Little Elephant and Magic Square 枚举左上角,计算其余两个位置的值,在\(3\times 3\) ...
- java SpringUtil获取bean
package com.whaty.framework.common.spring; import java.io.PrintStream; import org.springframework.be ...
- CE 文件读写操作
写入字符到文件中: // TODO: 写字符到文件 // 参数: CString类型的文件名FileName;char *类型的数据内容;unsigned int类型内容长度 // 返回: 成功返回T ...
- JSBinding+SharpKit / 菜单介绍
- WebServiceCaller
WebServiceCaller /* jonney 2015-09-19 */ using System; using System.Collections; using System.Collec ...
- 如何在IIS 7.5中部署Asp.Net MVC 5的网站
0 Sign in to vote 系统是 windwos 2008 已经安装.Net 4.0 和 .Net 4.5 已经安装MVC4 的需要文件,MVC5 找不见下载地方,求各位大哥告知一下在哪里可 ...