Codeforces1420 D. Rescue Nibel!(區間,組合數)
Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins… nothing happened.
Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door.
There are ? lamps with Spirit Tree’s light. Sein knows the time of turning on and off for the ?-th lamp — ?? and ?? respectively. To open the door you have to choose ? lamps in such a way that there will be a moment of time when they all will be turned on.
While Sein decides which of the ? lamps to pick, Ori is interested: how many ways there are to pick such ? lamps that the door will open? It may happen that Sein may be wrong and there are no such ? lamps. The answer might be large, so print it modulo 998244353.
Input
First line contains two integers ? and ? (1≤?≤3⋅105, 1≤?≤?) — total number of lamps and the number of lamps that must be turned on simultaneously.
Next ? lines contain two integers ?? ans ?? (1≤??≤??≤109) — period of time when ?-th lamp is turned on.
Output
Print one integer — the answer to the task modulo 998244353.
Examples
inputCopy
7 3
1 7
3 8
4 5
6 7
1 3
5 10
8 9
outputCopy
9
inputCopy
3 1
1 1
2 2
3 3
outputCopy
3
inputCopy
3 2
1 1
2 2
3 3
outputCopy
0
inputCopy
3 3
1 3
2 3
3 3
outputCopy
1
inputCopy
5 2
1 3
2 4
3 5
4 6
5 7
outputCopy
7
Note
In first test case there are nine sets of ? lamps: (1,2,3), (1,2,4), (1,2,5), (1,2,6), (1,3,6), (1,4,6), (2,3,6), (2,4,6), (2,6,7).
In second test case ?=1, so the answer is 3.
In third test case there are no such pairs of lamps.
In forth test case all lamps are turned on in a time 3, so the answer is 1.
In fifth test case there are seven sets of ? lamps: (1,2), (1,3), (2,3), (2,4), (3,4), (3,5), (4,5).
題意:
n個區間段。求多少種方式取
k
k
k個區間段使得其有交集。
思路:
先按左端點排序,然後對於對於每個區間段,尋找之前有多少個區間右端點值大於當前區間段左端點值。意思是在選擇當前區間段的前提下,有多少種方式在前面找k-1個區間與當前區間有交集。
然後組合數搞一下沒了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 7;
const int mod = 998244353;
struct Node {
int x,y;
}a[maxn];
int n,k;
int c[maxn * 5];
map<int,int>mp;
vector<int>vec;
int cmp(Node a,Node b) {
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
void add(int x,int v) {
while(x <= n * 5) {
c[x] += v;
x += x & -x;
}
}
int sum(int x) {
int res = 0;
while(x) {
res += c[x];
x -= x & -x;
}
return res;
}
ll fac[maxn],inv[maxn];
ll qpow(ll a,ll b)
{
ll res = 1;
while(b)
{
if(b & 1)
{
res = (res * a) % mod;
}
a = (a * a) % mod;
b = b >> 1;
}
return res % mod;
}
ll C(ll n,ll m)
{
if(m > n || m < 0)
return 0;
return fac[n] * ((inv[n - m] * inv[m]) % mod) % mod;
}
void init()
{
fac[0] = 1;
inv[0] = 1;
for(int i = 1;i <= maxn - 2;i++)
{
fac[i] = (fac[i - 1] * i) % mod;
inv[i] = qpow(fac[i],mod - 2);
}
}
int main() {
init();
scanf("%d%d",&n,&k);
mp.clear();
vec.clear();
for(int i = 1;i <= n * 5;i++) c[i] = 0;
for(int i = 1;i <= n;i++) {
scanf("%d%d",&a[i].x,&a[i].y);
vec.push_back(a[i].x);
vec.push_back(a[i].y);
vec.push_back(a[i].x - 1);
vec.push_back(a[i].y - 1);
}
sort(a + 1,a + 1 + n,cmp);
sort(vec.begin(),vec.end());
int cnt = 0;
for(int i = 0;i < vec.size();i++) {
if(!mp[vec[i]]) mp[vec[i]] = ++cnt;
}
ll ans = 0;
for(int i = 1;i <= n;i++) {
int num = sum(n * 5) - sum(mp[a[i].x - 1]);
if(num >= k - 1) {
ans += C(num,k - 1);
ans %= mod;
}
add(mp[a[i].y],1);
}
printf("%lld\n",ans);
return 0;
}
相關文章
- 區間統計 聚合函式組合器函式
- 【數學】組合數學 - 排列組合
- 統計將重疊區間合併成組的方案數.18098728
- 組合數學
- 合併區間
- 區間合併
- 組合數學筆記-排列與組合筆記
- 組合數問題
- lg組合計數
- 組合數字首和
- 6.24 區間合併
- 【數學】組合數學 - 卡特蘭數
- Python引數組合Python
- 《小 學 組 合 數 學》
- 【組合數學】組合數學簡介 ( 組合思想 2 : 數學歸納法 | 數學歸納法推廣 | 多重歸納思想 )
- 【區間dp】石子合併
- 組合,關聯,聚合的區別
- 不學無數——組合模式模式
- 計算組合數 (sdut oj)
- 組合計數思維題
- CF938E-組合數
- 組合數學 XKerror 筆記Error筆記
- 【POJ 2249】 Binomial Showdown 組合數學 排列組合計算
- 組合數學筆記-特殊計數數列筆記
- Redux實現組合計數器Redux
- 【總結】求組合數的方法
- Python 函式(引數組合)Python函式
- 【學習筆記】組合數學筆記
- 組合數學水題 $19$ 道
- 組合數學學習筆記筆記
- 亞太經合組織:2024年亞太經合組織地區趨勢
- 區間dp 合併石子問題
- 組合數取模的幾種方法--Exlucas&楊輝三角&組合
- 談一談自己對依賴、關聯、聚合和組合之間區別的理解
- 使用SQL按照區間進行分組SQL
- HDU 4497 GCD and LCM(拆素數+組合)GC
- HDU 2582 f(n) (組合數的gcd)GC
- 【演算法學習】組合數學演算法