跳到正文

题解

题解:CF1948F Rare Coins

Hooch约 11 分钟
原文
文章信息与版本

文章信息

发布
2024年3月16日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 11 分钟
题目
CF1948F

版本

  1. v2当前
  2. v1

可能更好的体验。

对于每个询问,可以把这 r−l+1r - l + 1 个袋子合并成一个 有 ∑i=lrai\sum\limits_{i = l}^r a_i 个金币和 ∑i=lrbi\sum\limits_{i = l}^r b_i 个银币的袋子。[l,r][l, r] 外的袋子同理也可以这样合并。

假设 suma=∑i=1nai,sumb=∑i=1nbisum_a = \sum\limits_{i = 1}^n a_i, sum_b = \sum\limits_{i = 1}^n b_i,ina=∑i=lrai,inb=∑l=1rbiin_a = \sum\limits_{i = l}^r a_i, in_b = \sum\limits_{l = 1}^r b_i, outa=suma−ina,outb=sumb−inbout_a = sum_a - in_a, out_b = sum_b - in_b。

那么原问题就是查询 ina+rand(inb)>outa+rand(outb)in_a + rand(in_b) > out_a + rand(out_b) 的概率,其中 rand(x)rand(x) 表示 xx 个银币时的随机取值。把上面的式子中符号两边的 randrand 移到同一边,得到:

rand(inb)−rand(outb)>outa−inarand(in_b) - rand(out_b) > out_a - in_a

观察到 inb+outb=sumbin_b + out_b = sum_b,那么尝试把 rand(inb)−rand(outb)rand(in_b) - rand(out_b) 变成 rand(inb)+rand(outb)rand(in_b) + rand(out_b)。只需两边同时加上 outbout_b,式子变成:

rand(inb)+rand(outb)>outa+outb−inarand(in_b) + rand(out_b) > out_a + out_b - in_a

接着又有 rand(inb)+rand(outb)=rand(inb+outb)=rand(sumb)rand(in_b) + rand(out_b) = rand(in_b + out_b) = rand(sum_b),所以最终转化成了:

rand(sumb)>outa+outb−inarand(sum_b) > out_a + out_b - in_a

其中 outa+outb−inaout_a + out_b - in_a 是可以 Θ(1)\Theta(1) 求得的,设 Q=outa+outb−inaQ = out_a + out_b - in_a,那么答案是:

∑x=Q+1sumb(sumbx)×(12)n\sum\limits_{x = Q + 1}^{sum_b} \binom{sum_b}{x} \times \left(\frac{1}{2}\right)^{n}

预处理组合数后缀和即可,复杂度为 O(n)O(n)。

code:

C++
#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;
}
原文
存档评论5 条抓取于