100%AC
2026-04-09 17:11:49
发布于:浙江
6阅读
0回复
0点赞
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 300010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
// 快速读入
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
#define mod 1000000007ll
ll fact[maxn], _fact[maxn], id[maxn], l[maxn], r[maxn], n[maxn], m[maxn], ans[maxn];
// 快速幂
ll fastpow(ll a, ll b)
{
ll t(a%mod), ans(1ll);
for(;b;b>>=1,t=t*t%mod)if(b&1)ans=ans*t%mod;
return ans;
}
// 组合数 C(n,m)
ll C(ll n, ll m)
{
if(n<0 or m<0 or m>n)return 0;
return fact[n]*_fact[m]%mod*_fact[n-m]%mod;
}
int main()
{
ll T=read(), p=read(), i, L, R, now, _2=500000004; // 2的逆元
rep(i,1,T)n[i]=read(), m[i]=read(), id[i]=i;
rep(i,1,T)l[i]=min(m[i],n[i])-1, r[i]=n[i]+m[i];
// 预处理阶乘、逆元
fact[0]=_fact[0]=1;
rep(i,1,maxn-1)fact[i]=fact[i-1]*i%mod, _fact[i]=fastpow(fact[i],mod-2);
ll S=sqrt(2e5);
// 莫队排序
sort(id+1,id+T+1,[S](ll a, ll b){return l[a]/S == l[b]/S ? r[a]<r[b] : l[a]/S < l[b]/S; });
// 莫队区间调整
L=R=0, now=1;
rep(i,1,T)
{
for(;L>l[id[i]];L--)
{
now = (now-C(R,L))%mod;
}
for(;R<r[id[i]];R++)
{
now = (now*2-C(R,L))%mod;
}
for(;L<l[id[i]];L++)
{
now = (now+C(R,L+1))%mod;
}
for(;R>r[id[i]];R--)
{
now = (now+C(R-1,L))*_2%mod;
}
// 计算答案
ans[id[i]] = ( n[id[i]]-m[id[i]]+max(0ll,m[id[i]]-n[id[i]]) + now*fastpow(C(n[id[i]]+m[id[i]],n[id[i]]),mod-2) )%mod;
}
// 输出
rep(i,1,T)printf("%lld\n",(ans[i]+mod)%mod);
return 0;
}
求求了点个赞吧
全部评论 1
数据大
1周前 来自 浙江
0






有帮助,赞一个