跳到正文

题解

FAOI-R2【B】A trip to Macao 题解

xhabc66约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
10
题目
P10036

版本

  1. v1当前

Subtask 0\texttt{Subtask 0}

爆搜。或者直接输出 mm。

Subtask 0.5\texttt{Subtask 0.5}

设 aia_i 为最终赢到 ii 澳元的方案数,cic_i 表示是否有面值为 ii 的筹码(有为 11,没有为 00),则 ana_n 为答案。

递推式:

a1=c1a_1=c_1 ai+1=(a⌈i+12⌉+…+ai−1+ai)+ci+1a_{i+1}=(a_{\lceil\frac{i+1}{2}\rceil}+\ldots+a_{i-1}+a_i)+c_{i+1}

时间 / 空间复杂度为 O(n2+m)/O(n+m)O(n^2+m)/O(n+m)。

Subtask 1\texttt{Subtask 1}

设 bib_i 为 aia_i 的前缀和(b1=a1b_1=a_1,bi=bi−1+aib_i=b_{i-1}+a_i),注意到 ⌈i+12⌉=⌊i2⌋+1\lceil\dfrac{i+1}{2}\rceil=\lfloor\dfrac{i}{2}\rfloor+1,则 bn−bn−1b_n-b_{n-1} 为答案。

b1=c1b_1=c_1 bi+1=(bi+bi−b⌊i2⌋)+ci+1b_{i+1}=(b_i+b_i-b_{\lfloor\frac{i}{2}\rfloor})+c_{i+1}

时间 / 空间复杂度为 O(n+m)/O(n+m)O(n+m)/O(n+m)。

Subtask 2\texttt{Subtask 2}

不难发现,求 bi+1b_{i+1} 时只需要 bib_i 及 b⌊i2⌋b_{\lfloor\frac{i}{2}\rfloor} 的值。于是,我们可以每次只存 bi,b⌊i2⌋,b⌊i4⌋,b⌊i8⌋,…,b1b_i,b_{\lfloor\frac{i}{2}\rfloor},b_{\lfloor\frac{i}{4}\rfloor},b_{\lfloor\frac{i}{8}\rfloor},\ldots,b_1 的值。例如:

ii存储的下标
1111
222,12,1
333,13,1
444,2,14,2,1
555,2,15,2,1
666,3,16,3,1
777,3,17,3,1
888,4,2,18,4,2,1
…\ldots…\ldots

不难发现,每次从右往前更新即可。时间 / 空间复杂度为 O(n+m)/O(m+log⁡n)O(n+m)/O(m+ \log n)。

参考代码(std)

C++
#include <bits/stdc++.h>
using namespace std;
const int B = 32, P = 1e9 + 7, M = 1e6 + 5;
int b[B], p[B], a[M];
int main()
{
    int n, m, s = 0;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        int t = i & (-i);
        for (int j = 1, k = 1, l = i; k <= t; j++, k <<= 1, l >>= 1)
        {
            b[j] <<= 1;
            b[j] -= b[j + 1];
            if (p[j] != m && a[p[j] + 1] == l) b[j]++, p[j]++;
            b[j] %= P;
            if (b[j] < 0) b[j] += P;
        }
        if (i == n - 1) s -= b[1];
        if (i == n) s += b[1];
    }
    cout << (s % P + P) % P;
    return 0;
}
原文