Mike and Geometry Problem

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121333#problem/I

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

Input

3 2

1 2

1 3

2 3

Output

5

Input

3 3

1 3

1 3

1 3

Output

3

Input

3 1

1 2

2 3

3 4

Output

6

Hint

题意:

横轴上有n个区间,每次取其中的k个区间,记录区间交集所覆盖的整点;

问对于所有的区间取法,一共覆盖了多少次整点;

题解:

实际上先求出每个整点被多少个区间所覆盖;

假设某点被m条边覆盖,则C(m, k)即为该点一共被覆盖的次数;

(若 m < k 则说明不可能处于k个区间的交集区);

前提:离散化各点! Map[l]++; Map[r+1]--;

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
LL k;
map<int,int> mp; LL x,y,gcd;
void ex_gcd(LL a,LL b)
{
if(!b) {x=1;y=0;gcd=a;}
else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}
}
LL f1[maxn],f2[maxn];
/*分子n!,f[i]为(i!)%mod的值*/
void F1()
{
f1[0]=1;
for(int i=1;i<maxn;i++)
f1[i]=(f1[i-1]*i)%mod;
}
/*分母m!,f[i]为(1/i!)%mod的值--逆元*/
void F2()
{
f2[0]=1;
for(int i=1;i<maxn;i++)
{
ex_gcd(i,mod);while(x<0) {x+=mod;y-=i;}
f2[i]=(f2[i-1]*(x%mod))%mod;
}
}
LL C_m_n(LL m,LL n)
{
/*ans=m!/(m-n)!n!*/
LL ans=(((f1[m]*f2[m-n])%mod)*f2[n])%mod;
return ans;
} int main(int argc, char const *argv[])
{
//IN; F1(); F2();
while(scanf("%d %I64d",&n,&k) != EOF)
{
mp.clear();
for(int i=1; i<=n; i++) {
LL x,y; scanf("%I64d %I64d", &x,&y);
mp[x]++;
mp[y+1]--;
} LL last = 0;
LL ans = 0, cur = 0;
map<int,int>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++) {
LL x = it->first, y = it->second;
if(cur >= k)
ans = (ans + C_m_n(cur, k)*(x-last)) % mod;
last = x;
cur += y;
} printf("%I64d\n", ans);
} return 0;
}

CodeForces 689E Mike and Geometry Problem (离散化+组合数)的更多相关文章

  1. CodeForces 689E Mike and Geometry Problem

    离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. codeforces 689E E. Mike and Geometry Problem(组合数学)

    题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...

  6. 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 ...

  7. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  8. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  9. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

随机推荐

  1. 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务(老罗学习笔记5)

    在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关系 ...

  2. 解决 “无法安装 Visual Studio 2010 Service Pack 1,因为此计算机的状态不支持”

    http://blog.csdn.net/davidhsing/article/details/8762621 无法安装Microsoft visual studio 2010 service pac ...

  3. heatmap.2

    heatmap.2 {gplots} R Documentation Enhanced Heat Map Description A heat map is a false color image ( ...

  4. voliecty indexOf的写法

    Velocity allows you to use all Java methods available in your objects. So just write as if it was Ja ...

  5. bzoj3413

    SAM好题,显然我们不能与每个后缀都去算LCP 考虑对询问串每一位算贡献,先构建出逆序构建自动机,这样我们得到了原串的后缀树(parent树) 根据parent树的定义,一个节点对应字符串出现的位置对 ...

  6. Asp.net 后台添加Meta标签方法

    Asp.net 后台添加Meta标签方法包括keywords,CSS.JS 下面是从Asp.net 后台添加CSS.JS.Meta标签的写法,我们这里写成函数方便以后使用.如果函数放在页面类中, Pa ...

  7. Java Web编程的主要组件技术——Hibernate入门

    参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...

  8. uva 10054 The Necklac(欧拉回路)

    明显的欧拉回路,把颜色作为点,建图后,做一遍欧拉回路.不过我是现学的,打印路径上纠结了一下,发现随着FindEuler()的递归调用的结束,不断把点压入栈中,从后向前打印,遇到"支路&quo ...

  9. HDU 3746 Cyclic Nacklace 环形项链(KMP,循环节)

    题意: 给一个字符串,问:要补多少个字符才能让其出现循环?出现循环是指循环节与字符串长度不相等.比如abc要补多个变成abcabc.若已经循环,输出0. 思路: 根据最小循环节的公式,当len%(le ...

  10. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...