codeforces(D. Rescue Nibel!)(排序,二分)

这篇博客展示了如何使用C++进行高效的数学计算,包括模运算、快速幂和前缀和,并结合排序算法处理数组。文章通过定义结构体`node`,实现了对数据的排序,并使用`lower_bound`函数进行区间查询。最后,代码演示了如何求解特定数学问题并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接
在这里插入图片描述

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while (c < '0' || c > '9') {if (c == '-')f = -1; c = getchar();}
    while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x * f;
}
using namespace std;

ll n,k,ans;
ll mod=998244353;
struct node
{
    ll x,y;
}e[300005];
ll c[300005],a[300005];
bool cmp(node a,node b)
{
    if(a.x<b.x){return 1;}
    return 0;
}
ll apow(ll a,ll b)
{
    ll s=1;
    while(b)
    {
        if(b&1){s=s*a%mod;}
        a=a*a%mod;
        b>>=1;
    }
    return s;
}
void init()
{
    c[k-1]=1;
    ll val=1,base=1;
    for(ll i=1;i<=k-1;i++){val=val*i%mod;}
    for(ll i=1;i<=k-1;i++){base=base*apow(i,mod-2)%mod;}
    for(ll i=k;i<=n;i++)
    {
        val=val*i%mod;
        val=val*apow(i-(k-1),mod-2)%mod;
        c[i]=val*base%mod;
    }
}
ll query(ll key)
{
    ll pos=lower_bound(a+1,a+1+n,key)-a;
    return n-pos+1;
}
int main()
{
    scanf("%lld %lld",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld %lld",&e[i].x,&e[i].y);
    }
    init();
    sort(e+1,e+1+n,cmp);
    for(int i=1;i<=n;i++){a[i]=e[i].y;}
    sort(a+1,a+1+n);
    for(int i=n;i>=1;i--)
    {
        ll cc=query(e[i].x);
        cc-=(n-i+1);
        if(cc>=(k-1))
        {
            ans=(ans+c[cc])%mod;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值