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;
}


發佈了336 篇原創文章 · 獲贊 29 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章