CF 361 E. Mike and Geometry Problem (排列组合+乘法逆元)

E. Mike and Geometry Problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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 n and k and nclosed 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 kof 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.

Examples
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
Note

In the first example:

;

;

.

So the answer is 2 + 1 + 2 = 5.


题目求线段点重合大于K次的组合次数,求和即可。类似两点问题,然后就是组合数快速求解,利用乘法逆元进行预处理。

#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mem(a,t) memset(a,t,sizeof(a))
#define all(v) v.begin(),v.end()
#define N 200005
const int mod=1e9+7;
map<int,int>p;
int f[N];
int c[N];
int cal(int n,int k)
{
    int ans=(1ll*f[n]*c[k])%mod;
    return (1ll*ans*c[n-k])%mod;
}
int pow(int a,int b,int mod){
    int ans=1;
    while(b){
        if(b&1)
            ans=(1ll*a*ans)%mod;
        a=(1LL*a*a)%mod;
        b>>=1;
    }
    return ans;
}
void init(int n)
{
    f[0]=c[0]=1;
    rep(i,1,n+1) f[i]=(1ll*f[i-1]*i)%mod,c[i]=pow(f[i],mod-2,mod);
}
int main()
{
    int n,k,l,r;
    scanf("%d%d",&n,&k);
    rep(i,0,n){
        scanf("%d%d",&l,&r);
        ++p[l];
        --p[r+1];
    }
    init(n);
    map<int,int>::iterator it;
    l=p.begin()->fi;
    int num=0;
    LL ans=0;
    for(it=p.begin();it!=p.end();++it){
        r=it->fi;
        if(num>=k){
            ans+=1ll*cal(num,k)*(r-l)%mod;
            if(ans>=mod)
                ans-=mod;
        }
        num+=p[r];
        l=r;
    }
    cout<<ans<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值