「果壳杯」 ROUND 46 (Div. 2) 题解

YIZHIYANG初来乍到 2026-11-26 15:43:38 18 浏览 0 点赞 0 收藏

本帖汇总本场比赛各题的解法与参考程序。

比赛入口

  • A. 奇偶权值
  • B. 炸弹游戏
  • C. 旋转色轮
  • D. 裂隙色轮
  • E. 褪色圆环

A. 奇偶权值

设奇数位置有 o=⌈n/2⌉o=\lceil n/2\rceil 个,偶数位置有 e=⌊n/2⌋e=\lfloor n/2\rfloor 个。把两段数位和的乘积展开为

f(s)=∑i 为奇数位置∑j 为偶数位置didj.f(s)=\sum_{i\text{ 为奇数位置}}\sum_{j\text{ 为偶数位置}}d_id_j.

固定一对位置 (i,j)(i,j),枚举这两位的数字时,乘积总和为

(∑a=09a)(∑b=09b)=452=2025.\left(\sum_{a=0}^{9}a\right)\left(\sum_{b=0}^{9}b\right)=45^2=2025.

其余 n−2n-2 位各有十种选择,因此这对位置在全部数字串中的贡献是 2025⋅10n−22025\cdot10^{n-2}。共有 oeoe 对,答案就是

oe⋅2025⋅10n−2(mod109+7).oe\cdot2025\cdot10^{n-2}\pmod{10^9+7}.

n=1n=1 时没有偶数位置,所有权值均为 00,单独输出 00,避免出现负指数。用快速幂计算 10n−210^{n-2},每组时间复杂度 O(log⁡n)O(\log n),额外空间复杂度 O(1)O(1)。

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const ll MOD=1000000007;
ll power(ll a,ll b) {
    ll r=1; for (;b;b>>=1,a=a*a%MOD) if (b&1) r=r*a%MOD;
    return r;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T; cin>>T;
    while (T--) {
        ll n; cin>>n;
        if (n==1) { cout<<0<<'\n'; continue; }
        cout<<((n+1)/2%MOD)*(n/2%MOD)%MOD*2025%MOD*power(10,n-2)%MOD<<'\n';
    }
}

B. 炸弹游戏

记 pn(i)p_n(i) 为当前队列有 nn 个人、某人排在第 ii 位时,他最终留下的概率。队列中的具体姓名不影响概率,只需关心相对位置。

对于 i≥2i\ge2,看第一次操作:队首以 1/21/2 的概率被淘汰,此人转为 n−1n-1 人队列的第 i−1i-1 位;队首以 1/21/2 的概率移到队尾,此人转为 nn 人队列的第 i−1i-1 位。所以

pn(i)=pn−1(i−1)+pn(i−1)2.p_n(i)=\frac{p_{n-1}(i-1)+p_n(i-1)}2.

队首自身只有被移到队尾才可能生存,因此 pn(1)=pn(n)/2p_n(1)=p_n(n)/2。将上面的递推展开并代入这个首尾关系,可得

pn(1)=∑j=1n−12j−1pn−1(j)2n−1.p_n(1)=\frac{\sum_{j=1}^{n-1}2^{j-1}p_{n-1}(j)}{2^n-1}.

从 p1(1)=1p_1(1)=1 开始,先用这个公式算出每一行的第一项,再从左到右计算其余各项,即可得到 pNp_N。第一次操作的两种结果互斥且覆盖全部可能,这些等式因而完整描述了所求概率;首尾方程的系数 2n−12^n-1 在本题范围内可逆,从而每一行被唯一确定。

所有除法转为模意义下乘逆元。总时间复杂度 O(N2+Nlog⁡P)O(N^2+N\log P),滚动保存相邻两行,空间复杂度 O(N)O(N),其中 P=998244353P=998244353。

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const ll MOD=998244353, INV2=(MOD+1)/2;
ll power(ll a,ll b) {
    ll r=1; for (;b;b>>=1,a=a*a%MOD) if (b&1) r=r*a%MOD;
    return r;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin>>N; vector<ll> pw(N+1,1),prev(N+1),cur(N+1);
    for (int i=1;i<=N;++i) pw[i]=pw[i-1]*2%MOD;
    prev[1]=1;
    for (int n=2;n<=N;++n) {
        ll weighted=0;
        for (int j=1;j<n;++j) weighted=(weighted+prev[j]*pw[j-1])%MOD;
        cur[1]=weighted*power((pw[n]+MOD-1)%MOD,MOD-2)%MOD;
        for (int i=2;i<=n;++i) cur[i]=(prev[i-1]+cur[i-1])*INV2%MOD;
        prev.swap(cur);
    }
    for (int i=1;i<=N;++i) cout<<prev[i]<<(i==N?'\n':' ');
}

C. 旋转色轮

先确定中心颜色,有 NN 种选择。外环不能再使用中心颜色,剩下 q=N−1q=N-1 种颜色,需要给长度为 MM 的环做相邻不同的染色,并将旋转视为相同。

先求有标号环的染色数。记长度为 dd 的环用 qq 种颜色合法染色的数量为 Cd(q)C_d(q)。固定第一点颜色,按最后一点是否与第一点相同做递推,或对完全图的转移矩阵求迹,可以得到

Cd(q)=(q−1)d+(−1)d(q−1).C_d(q)=(q-1)^d+(-1)^d(q-1).

这个公式也给出 C1(q)=0C_1(q)=0:缩成一个位置时,环边成为自环,不能合法染色。

对全部 MM 种旋转应用 Burnside 引理。旋转 rr 格后,位置被分成 d=gcd⁡(M,r)d=\gcd(M,r) 个轨道。不变染色恰好是一个长度为 dd 的周期重复,合法性等价于给长度为 dd 的环合法染色。因此这个旋转的不动点数为 Cd(q)C_d(q)。

满足 gcd⁡(M,r)=d\gcd(M,r)=d 的旋转恰有 φ(M/d)\varphi(M/d) 个。于是答案为

NM∑d∣Mφ(M/d)((N−2)d+(−1)d(N−2))(modP).\frac{N}{M}\sum_{d\mid M}\varphi(M/d)\left((N-2)^d+(-1)^d(N-2)\right)\pmod P.

M≤1018<PM\le10^{18}<P,所以除以 MM 可以乘上模逆元。先用 Miller–Rabin 和 Pollard Rho 分解 MM,递归枚举约数 k=M/dk=M/d,并由素因子指数直接计算 φ(k)\varphi(k)。每个约数用快速幂求相应项。乘法使用 __uint128_t,避免模数接近 2612^{61} 时溢出;对计数模数 P=261−1P=2^{61}-1,可用高低位相加完成快速取模。

设 τ(M)\tau(M) 为约数个数,计数部分时间复杂度 O(τ(M)log⁡M)O(\tau(M)\log M)。整数分解使用随机化 Pollard Rho,寻找一个素因子 pp 的期望迭代次数为 O(p)O(\sqrt p);这里不使用试除到 M\sqrt M 的算法。递归与素因子表占用 O(log⁡M)O(\log M) 空间。

#include <bits/stdc++.h>
using namespace std;

using u64 = unsigned long long;
using u128 = __uint128_t;
const u64 P = (1ULL << 61) - 1;
u64 gcd64(u64 a, u64 b) { while (b) { u64 t=a%b; a=b; b=t; } return a; }
u64 mul(u64 a, u64 b, u64 mod) { return (u128)a*b%mod; }
u64 power(u64 a, u64 b, u64 mod) {
    u64 r=1; for (;b;b>>=1,a=mul(a,a,mod)) if (b&1) r=mul(r,a,mod);
    return r;
}
bool prime(u64 n) {
    if (n<2) return false;
    for (u64 p : {2ULL,3ULL,5ULL,7ULL,11ULL,13ULL,17ULL,19ULL,23ULL,29ULL,31ULL,37ULL})
        if (n%p==0) return n==p;
    u64 d=n-1; int s=0; while (!(d&1)) { d>>=1; ++s; }
    for (u64 a : {2ULL,325ULL,9375ULL,28178ULL,450775ULL,9780504ULL,1795265022ULL}) {
        if (a%n==0) continue;
        u64 x=power(a%n,d,n); if (x==1 || x==n-1) continue;
        bool ok=false;
        for (int j=1;j<s;++j) { x=mul(x,x,n); if (x==n-1) { ok=true; break; } }
        if (!ok) return false;
    }
    return true;
}
mt19937_64 rng(712367821);
u64 rho(u64 n) {
    if (n%2==0) return 2;
    if (n%3==0) return 3;
    while (true) {
        u64 y=rng()%(n-1)+1,c=rng()%(n-1)+1,g=1,r=1,q=1,x=0,ys=0;
        auto f=[&](u64 v) { return (mul(v,v,n)+c)%n; };
        while (g==1) {
            x=y; for (u64 i=0;i<r;++i) y=f(y);
            for (u64 k=0;k<r && g==1;k+=128) {
                ys=y;
                for (u64 i=0;i<min(128ULL,r-k);++i) {
                    y=f(y); q=mul(q,x>y?x-y:y-x,n);
                }
                g=gcd64(q,n);
            }
            r<<=1;
        }
        if (g==n) {
            do { ys=f(ys); g=gcd64(x>ys?x-ys:ys-x,n); } while (g==1);
        }
        if (g!=n) return g;
    }
}
void factor(u64 n, map<u64,int>& f) {
    if (n==1) return;
    if (prime(n)) { ++f[n]; return; }
    u64 d=rho(n); factor(d,f); factor(n/d,f);
}
u64 mulP(u64 a,u64 b) {
    u128 z=(u128)a*b; u64 x=(u64)(z&P)+(u64)(z>>61);
    return x>=P?x-P:x;
}
u64 powerP(u64 a,u64 b) {
    u64 r=1; for (;b;b>>=1,a=mulP(a,a)) if (b&1) r=mulP(r,a);
    return r;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T; cin>>T;
    while (T--) {
        u64 N,M; cin>>N>>M; map<u64,int> f; factor(M,f);
        vector<pair<u64,int>> fs(f.begin(),f.end());
        u64 base=(N%P+P-2)%P, sum=0;
        function<void(int,u64,u64)> enumerate=[&](int at,u64 k,u64 phi) {
            if (at==(int)fs.size()) {
                u64 d=M/k, term=powerP(base,d);
                if (d&1) term=(term+P-base)%P; else term=(term+base)%P;
                sum=(sum+mulP(phi%P,term))%P; return;
            }
            u64 p=fs[at].first,pw=1,ph=1;
            for (int e=0;e<=fs[at].second;++e) {
                enumerate(at+1,k*pw,phi*ph);
                if (e<fs[at].second) { pw*=p; ph=e==0?p-1:ph*p; }
            }
        };
        enumerate(0,1,1);
        cout<<mulP(mulP(N%P,sum),powerP(M,P-2))<<'\n';
    }
}

D. 裂隙色轮

把当前固定颜色的位置按环上的顺序排列。相邻两个固定点之间是一段路径;不同路径内部的自由位置互不重叠,且端点颜色已确定,因此各段的方案数相乘就是整个环的方案数。

先求路径计数。设路径有 dd 条边,固定起点颜色,记终点也取起点颜色时的方案数为 AdA_d,终点取某个指定不同颜色时的方案数为 BdB_d。最后一步按前一个位置的颜色分类,得到

Ad+1=(N−1)Bd,Bd+1=Ad+(N−2)Bd,A_{d+1}=(N-1)B_d,\qquad B_{d+1}=A_d+(N-2)B_d,

初值为 A0=1,B0=0A_0=1,B_0=0。由此有 Ad−Bd=(−1)dA_d-B_d=(-1)^d,并且 Ad+(N−1)Bd=(N−1)dA_d+(N-1)B_d=(N-1)^d,所以

Ad=(N−1)d+(N−1)(−1)dN,Bd=(N−1)d−(−1)dN.A_d=\frac{(N-1)^d+(N-1)(-1)^d}{N},\qquad B_d=\frac{(N-1)^d-(-1)^d}{N}.

模数记为 P=998244353P=998244353。若 N≢0(modP)N\not\equiv0\pmod P,直接乘 NN 的逆元。如果 N=PN=P,不能做这个除法。将递推中的 NN 取模为 00 后归纳可得

Ad=(−1)d(1−d),Bd=(−1)d−1d(modP).A_d=(-1)^d(1-d),\qquad B_d=(-1)^{d-1}d\pmod P.

用有序映射维护固定点。给每个固定点保存“从它顺时针走到下一个固定点”的路径因子,根据两端颜色是否相同选用 AdA_d 或 BdB_d。只有一个固定点时,它到自己的路径长度为 MM,因子为 AMA_M。没有固定点时,答案直接为环的染色多项式 (N−1)M+(−1)M(N−1)(N-1)^M+(-1)^M(N-1)。

插入固定点只会把一段拆成两段,删除只会把两段合成一段,改色只影响左右两段。读完操作后压缩所有可能修改的位置,用乘积线段树保存这些因子;未固定的位置因子为 11。这样不需要除去旧因子,因子为 00 时也能正确更新。每次完成局部修改后,根结点的乘积就是答案。

预处理 (N−1)2i(N-1)^{2^i},每个路径因子在 O(log⁡M)O(\log M) 内计算。总时间复杂度 O(Q(log⁡Q+log⁡M))O(Q(\log Q+\log M)),空间复杂度 O(Q+log⁡M)O(Q+\log M)。

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const ll MOD=998244353;
ll power(ll a,ll b) { ll r=1; for (;b;b>>=1,a=a*a%MOD) if (b&1) r=r*a%MOD; return r; }
struct ProductTree {
    int size=1; vector<ll> t;
    ProductTree(int n) { while (size<n) size<<=1; t.assign(2*size,1); }
    void setValue(int p,ll v) {
        p+=size; t[p]=v;
        while (p>>=1) t[p]=t[p<<1]*t[p<<1|1]%MOD;
    }
    ll product() const { return t[1]; }
};
struct Op { int type,color; ll x; };
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    ll N,M; int Q; cin>>N>>M>>Q;
    vector<Op> ops(Q); vector<ll> coord;
    for (auto &o:ops) {
        cin>>o.type>>o.x; o.color=0; if (o.type==1) cin>>o.color; coord.push_back(o.x);
    }
    sort(coord.begin(),coord.end()); coord.erase(unique(coord.begin(),coord.end()),coord.end());
    ProductTree tree(coord.size()); map<ll,int> fixed;
    ll nm=N%MOD, b=(N-1)%MOD, bits[64]; bits[0]=b;
    for (int i=1;i<64;++i) bits[i]=bits[i-1]*bits[i-1]%MOD;
    auto powB=[&](ll d) { ll z=1; int bit=0; while (d) { if (d&1) z=z*bits[bit]%MOD; d>>=1; ++bit; } return z; };
    ll invN=nm?power(nm,MOD-2):0;
    auto gap=[&](ll d,bool same) {
        ll sign=d&1?MOD-1:1;
        if (nm==0) {
            ll dm=d%MOD;
            return same?sign*((1-dm+MOD)%MOD)%MOD:sign*((MOD-dm)%MOD)%MOD;
        }
        ll z=powB(d);
        if (same) return (z+b*sign)%MOD*invN%MOD;
        return (z-sign+MOD)%MOD*invN%MOD;
    };
    auto index=[&](ll x) { return lower_bound(coord.begin(),coord.end(),x)-coord.begin(); };
    auto prevIt=[&](map<ll,int>::iterator it) {
        if (it==fixed.begin()) it=fixed.end(); return --it;
    };
    auto refresh=[&](map<ll,int>::iterator it) {
        auto jt=next(it); if (jt==fixed.end()) jt=fixed.begin();
        ll d=jt->first>it->first?jt->first-it->first:M-it->first+jt->first;
        tree.setValue(index(it->first),gap(d,it->second==jt->second));
    };
    ll empty=(powB(M)+b*(M&1?MOD-1:1))%MOD;
    for (const auto &o:ops) {
        auto it=fixed.find(o.x);
        if (o.type==1) {
            if (it!=fixed.end()) {
                it->second=o.color; refresh(it);
                if (fixed.size()>1) refresh(prevIt(it));
            } else if (fixed.empty()) {
                auto inserted=fixed.emplace(o.x,o.color).first; refresh(inserted);
            } else {
                auto jt=fixed.lower_bound(o.x); if (jt==fixed.end()) jt=fixed.begin();
                auto pt=prevIt(jt); auto inserted=fixed.emplace(o.x,o.color).first;
                refresh(pt); refresh(inserted);
            }
        } else if (it!=fixed.end()) {
            tree.setValue(index(o.x),1);
            if (fixed.size()==1) fixed.erase(it);
            else { auto pt=prevIt(it); fixed.erase(it); refresh(pt); }
        }
        cout<<(fixed.empty()?empty:tree.product())<<'\n';
    }
}

E. 褪色圆环

颜色数最多为 55,可以直接把颜色作为转移状态。对位置 ii 定义一个 N×NN\times N 矩阵 TiT_i:

Ti[a][b]=[a≠b] [b 在位置 i 的允许集合中].T_i[a][b]=[a\ne b]\,[b\text{ 在位置 }i\text{ 的允许集合中}].

它表示从前一个位置的颜色 aa 转移到当前位置的颜色 bb。设所有位置的转移矩阵按顺序相乘为 T=T1T2⋯TMT=T_1T_2\cdots T_M,那么答案等于 tr⁡(T)=∑cT[c][c]\operatorname{tr}(T)=\sum_cT[c][c]。

展开矩阵乘法的一项,等于沿环依次选择每个位置的颜色;各位置的允许集合和各相邻位置颜色不同的条件,都在对应矩阵中检查。取对角元要求走完一圈后的颜色等于出发颜色,恰好补上首尾的相邻约束。每个合法染色对应唯一一项,故矩阵迹不重不漏地统计全部方案。

用线段树维护区间矩阵乘积,左右儿子的合并顺序必须保持为“左矩阵乘右矩阵”,不能交换。模板对应的单点矩阵可以预先生成。修改一个位置绑定的模板,只需替换一个叶子并向上更新;根矩阵的迹就是修改后的答案。

一次矩阵相乘需要 O(N3)O(N^3) 时间,建树 O(MN3)O(MN^3),每次修改 O(N3log⁡M)O(N^3\log M),空间复杂度 O(MN2+SN2)O(MN^2+SN^2)。实现中每个矩阵元素将至多五个乘积累加后再取模;上界小于 5P2<2635P^2<2^{63},可用 long long 安全累加。

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const ll MOD=998244353;
int colors;
struct Matrix { int a[25]{}; };
Matrix multiply(const Matrix& a,const Matrix& b) {
    Matrix c;
    for (int i=0;i<colors;++i) for (int j=0;j<colors;++j) {
        ll sum=0;
        for (int k=0;k<colors;++k) sum+=(ll)a.a[i*5+k]*b.a[k*5+j];
        c.a[i*5+j]=sum%MOD;
    }
    return c;
}
Matrix identity() {
    Matrix a; for (int i=0;i<colors;++i) a.a[i*5+i]=1; return a;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int m,s,q; cin>>colors>>m>>s>>q;
    vector<Matrix> templ(s+1);
    for (int t=1;t<=s;++t) {
        int c; cin>>c; vector<char> allow(colors,false);
        while (c--) { int x; cin>>x; allow[x-1]=true; }
        for (int a=0;a<colors;++a) for (int b=0;b<colors;++b)
            templ[t].a[a*5+b]=(a!=b && allow[b]);
    }
    int size=1; while (size<m) size<<=1;
    vector<Matrix> seg(2*size,identity());
    for (int i=0;i<m;++i) { int t; cin>>t; seg[size+i]=templ[t]; }
    for (int p=size-1;p>=1;--p) seg[p]=multiply(seg[p<<1],seg[p<<1|1]);
    while (q--) {
        int x,t; cin>>x>>t; int p=size+x-1; seg[p]=templ[t];
        while (p>>=1) seg[p]=multiply(seg[p<<1],seg[p<<1|1]);
        ll ans=0; for (int c=0;c<colors;++c) ans+=seg[1].a[c*5+c];
        cout<<ans%MOD<<'\n';
    }
}

评论

0 条
还没有评论。