CF1948F 题解
对于每个询问,可以把这
假设
那么原问题就是查询
观察到
接着又有
其中
预处理组合数后缀和即可,复杂度为
code:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 2E6 + 5;
template <class T>
T power(T a, long long b) {
T res = 1;
for (; b; b >>= 1, a *= a) {
if (b & 1)
res *= a;
} return res;
}
#define C constexpr
#define FC friend constexpr
#define OP operator
template <long long mod>
class ModLL {
public:
long long n;
static long long Mod;
C ModLL() : n{} {}
C ModLL(long long x) : n(norm(x % getmod())) {}
C long long norm(long long x) {
if (x >= getmod()) x %= getmod();
if (x <= -getmod()) x %= getmod();
if (x < 0) x += getmod();
return x;
}
C long long getmod() {return (mod > 0 ? mod : Mod);}
explicit C OP long long() const {return n;}
C ModLL OP -() const {ModLL a; a.n = norm(getmod() - n); return a;}
C ModLL inv() {assert(n != 0); return power((*this), getmod() - 2);}
C ModLL &OP += (ModLL w) & {n = norm( n + w.n); return (*this);}
C ModLL &OP -= (ModLL w) & {n = norm( n - w.n); return (*this);}
C ModLL &OP *= (ModLL w) & {n = norm( 1LL * n * w.n % getmod()); return (*this);}
C ModLL &OP /= (ModLL w) & {return (*this) *= w.inv();}
FC ModLL OP + (ModLL a, ModLL b) {ModLL res = a; res += b; return res;}
FC ModLL OP - (ModLL a, ModLL b) {ModLL res = a; res -= b; return res;}
FC ModLL OP * (ModLL a, ModLL b) {ModLL res = a; res *= b; return res;}
FC ModLL OP / (ModLL a, ModLL b) {ModLL res = a; res /= b; return res;}
FC bool OP == (ModLL a, ModLL b) {return (a.n == b.n);}
FC bool OP != (ModLL a, ModLL b) {return (a.n != b.n);}
FC istream &OP >> (istream &is, ModLL &a) {
long long x = 0; is >> x;
a = ModLL(x); return is;
}
FC ostream &OP << (ostream &os, const ModLL &a) {return (os << (a.n));}
} ;
template <int mod>
class ModInt {
public:
int n;
static int Mod;
C ModInt() : n{} {}
C ModInt(int x) : n(norm(x % getmod())) {}
C int norm(int x) {
if (x >= getmod()) x %= getmod();
if (x <= -getmod()) x %= getmod();
if (x < 0) x += getmod();
return x;
}
C static int getmod() {return (mod > 0 ? mod : Mod);}
explicit C OP int() const {return n;}
C ModInt OP -() const {ModInt a; a.n = norm(getmod() - n); return a;}
C ModInt inv() const {assert(n != 0); return power((*this), getmod() - 2);}
C ModInt &OP += (ModInt w) & {n = norm( n + w.n); return (*this);}
C ModInt &OP -= (ModInt w) & {n = norm( n - w.n); return (*this);}
C ModInt &OP *= (ModInt w) & {n = norm( 1LL * n * w.n % getmod()); return (*this);}
C ModInt &OP /= (ModInt w) & {return (*this) *= w.inv();}
FC ModInt OP + (ModInt a, ModInt b) {ModInt res = a; res += b; return res;}
FC ModInt OP - (ModInt a, ModInt b) {ModInt res = a; res -= b; return res;}
FC ModInt OP * (ModInt a, ModInt b) {ModInt res = a; res *= b; return res;}
FC ModInt OP / (ModInt a, ModInt b) {ModInt res = a; res /= b; return res;}
FC bool OP == (ModInt a, ModInt b) {return (a.n == b.n);}
FC bool OP != (ModInt a, ModInt b) {return (a.n != b.n);}
FC istream &OP >> (istream &is, ModInt &a) {
int x = 0; is >> x;
a = ModInt(x); return is;
}
FC ostream &OP << (ostream &os, const ModInt &a) {return (os << (a.n));}
} ;
template <>
long long ModLL <0> :: Mod = (long long)(1E18) + 9;
template <>
int ModInt <0> :: Mod = 998244353;
using P = ModInt <998244353>;
#undef C
#undef FC
#undef OP
signed main(void) {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, q; cin >> n >> q;
vector <int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
vector <int> suma(n + 1), sumb(n + 1);
for (int i = 1; i <= n; ++i) {
suma[i] = suma[i - 1] + a[i];
sumb[i] = sumb[i - 1] + b[i];
}
vector <P> comb(sumb[n] + 1), fac(sumb[n] + 1); fac[0] = 1;
for (int i = 1; i <= sumb[n]; ++i) fac[i] = fac[i - 1] * i;
comb[sumb[n]] = 1;
for (int i = sumb[n] - 1; i >= 0; --i)
comb[i] = comb[i + 1] + (fac[sumb[n]] / fac[i] / fac[sumb[n] - i]);
P div = power(P(2), sumb[n]);
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
--l;
int in_a = suma[r] - suma[l], in_b = sumb[r] - sumb[l];
int out_a = suma[n] - in_a, out_b = sumb[n] - in_b;
int Q = out_a + out_b - in_a;
if (Q >= sumb[n]) cout << 0 << ' ';
else if (Q < 0) cout << 1 << ' ';
else cout << comb[Q + 1] / div << ' ';
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具