CF689E:Mike and Geometry Problem(组合数)
2017-08-23 22:22
288 查看
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 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?
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
output
input
output
input
output
Note
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
题意:给N个区间,问所有这N个区间组成的K元组区间的公共部分长度之和。
思路:每个点分开考虑,对于某个点被覆盖的次数>=k就贡献C(x, k), 数的范围太大用map。
# include <bits/stdc++.h>
# define A first
# define B second
using namespace std;
typedef long long LL;
const int maxn = 2e5;
const LL mod = 1e9+7;
LL inv[maxn+30]={1,1}, fac[maxn+30]={1,1}, fi[maxn+30]= {1,1};
void init()
{
for(int i=2; i<=maxn; ++i)
{
fac[i] = i*fac[i-1]%mod;
inv[i] = (mod-mod/i)*inv[mod%i]%mod;
fi[i] = fi[i-1]*inv[i]%mod;
}
}
LL C(LL n, LL m)
{
return fac
*fi[m]%mod*fi[n-m]%mod;
}
map<int,int>m;
int main()
{
init();
int n, k, a, b;
scanf("%d%d",&n,&k);
for(int i=0; i<n; ++i)
{
scanf("%d%d",&a,&b);
++m[a];--m[b+1];
}
int l = m.begin()->A;
LL sum = 0, ans = 0;
for(auto it : m)
{
int dis = it.A - l;
if(sum >= k)
{
ans += C(sum, (LL)k)*dis;
ans %= mod;
}
sum += it.B;
l = it.A;
}
return 0*printf("%I64d\n",ans);;
}
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 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?
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 33 4
output
6
Note
In the first example:
;
;
.
So the answer is 2 + 1 + 2 = 5.
题意:给N个区间,问所有这N个区间组成的K元组区间的公共部分长度之和。
思路:每个点分开考虑,对于某个点被覆盖的次数>=k就贡献C(x, k), 数的范围太大用map。
# include <bits/stdc++.h>
# define A first
# define B second
using namespace std;
typedef long long LL;
const int maxn = 2e5;
const LL mod = 1e9+7;
LL inv[maxn+30]={1,1}, fac[maxn+30]={1,1}, fi[maxn+30]= {1,1};
void init()
{
for(int i=2; i<=maxn; ++i)
{
fac[i] = i*fac[i-1]%mod;
inv[i] = (mod-mod/i)*inv[mod%i]%mod;
fi[i] = fi[i-1]*inv[i]%mod;
}
}
LL C(LL n, LL m)
{
return fac
*fi[m]%mod*fi[n-m]%mod;
}
map<int,int>m;
int main()
{
init();
int n, k, a, b;
scanf("%d%d",&n,&k);
for(int i=0; i<n; ++i)
{
scanf("%d%d",&a,&b);
++m[a];--m[b+1];
}
int l = m.begin()->A;
LL sum = 0, ans = 0;
for(auto it : m)
{
int dis = it.A - l;
if(sum >= k)
{
ans += C(sum, (LL)k)*dis;
ans %= mod;
}
sum += it.B;
l = it.A;
}
return 0*printf("%I64d\n",ans);;
}
相关文章推荐
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem (离散化)
- [Codeforces 689E] Mike and Geometry Problem (贡献计数+静态区间更新)
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元
- Codeforces Round #361 (Div. 2)E. Mike and Geometry Problem
- codeforces 361 E - Mike and Geometry Problem
- CF 361 E. Mike and Geometry Problem (排列组合+乘法逆元)
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
- codeforces689E: Mike and Geometry Problem
- Codeforces #361 E. Mike and Geometry Problem 数学
- [CF798C] Mike and gcd problem(规律,gcd)
- D - Mike and gcd problem
- Codeforces 798C:Mike and gcd problem
- codeforces 798C - Mike and gcd problem 贪心
- CF798 C. Mike and gcd problem
- C. Mike and gcd problem
- 单调栈经典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)
- 【算法系列学习】codeforces C. Mike and gcd problem
- Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)
- Codeforces Round #410 (Div. 2) Mike and gcd problem 思维