「果壳杯」 ROUND 46 (Div. 2) 题解
本帖汇总本场比赛各题的解法与参考程序。
- A. 奇偶权值
- B. 炸弹游戏
- C. 旋转色轮
- D. 裂隙色轮
- E. 褪色圆环
A. 奇偶权值
设奇数位置有 个,偶数位置有 个。把两段数位和的乘积展开为
固定一对位置 ,枚举这两位的数字时,乘积总和为
其余 位各有十种选择,因此这对位置在全部数字串中的贡献是 。共有 对,答案就是
时没有偶数位置,所有权值均为 ,单独输出 ,避免出现负指数。用快速幂计算 ,每组时间复杂度 ,额外空间复杂度 。
#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. 炸弹游戏
记 为当前队列有 个人、某人排在第 位时,他最终留下的概率。队列中的具体姓名不影响概率,只需关心相对位置。
对于 ,看第一次操作:队首以 的概率被淘汰,此人转为 人队列的第 位;队首以 的概率移到队尾,此人转为 人队列的第 位。所以
队首自身只有被移到队尾才可能生存,因此 。将上面的递推展开并代入这个首尾关系,可得
从 开始,先用这个公式算出每一行的第一项,再从左到右计算其余各项,即可得到 。第一次操作的两种结果互斥且覆盖全部可能,这些等式因而完整描述了所求概率;首尾方程的系数 在本题范围内可逆,从而每一行被唯一确定。
所有除法转为模意义下乘逆元。总时间复杂度 ,滚动保存相邻两行,空间复杂度 ,其中 。
#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. 旋转色轮
先确定中心颜色,有 种选择。外环不能再使用中心颜色,剩下 种颜色,需要给长度为 的环做相邻不同的染色,并将旋转视为相同。
先求有标号环的染色数。记长度为 的环用 种颜色合法染色的数量为 。固定第一点颜色,按最后一点是否与第一点相同做递推,或对完全图的转移矩阵求迹,可以得到
这个公式也给出 :缩成一个位置时,环边成为自环,不能合法染色。
对全部 种旋转应用 Burnside 引理。旋转 格后,位置被分成 个轨道。不变染色恰好是一个长度为 的周期重复,合法性等价于给长度为 的环合法染色。因此这个旋转的不动点数为 。
满足 的旋转恰有 个。于是答案为
,所以除以 可以乘上模逆元。先用 Miller–Rabin 和 Pollard Rho 分解 ,递归枚举约数 ,并由素因子指数直接计算 。每个约数用快速幂求相应项。乘法使用 __uint128_t,避免模数接近 时溢出;对计数模数 ,可用高低位相加完成快速取模。
设 为约数个数,计数部分时间复杂度 。整数分解使用随机化 Pollard Rho,寻找一个素因子 的期望迭代次数为 ;这里不使用试除到 的算法。递归与素因子表占用 空间。
#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. 裂隙色轮
把当前固定颜色的位置按环上的顺序排列。相邻两个固定点之间是一段路径;不同路径内部的自由位置互不重叠,且端点颜色已确定,因此各段的方案数相乘就是整个环的方案数。
先求路径计数。设路径有 条边,固定起点颜色,记终点也取起点颜色时的方案数为 ,终点取某个指定不同颜色时的方案数为 。最后一步按前一个位置的颜色分类,得到
初值为 。由此有 ,并且 ,所以
模数记为 。若 ,直接乘 的逆元。如果 ,不能做这个除法。将递推中的 取模为 后归纳可得
用有序映射维护固定点。给每个固定点保存“从它顺时针走到下一个固定点”的路径因子,根据两端颜色是否相同选用 或 。只有一个固定点时,它到自己的路径长度为 ,因子为 。没有固定点时,答案直接为环的染色多项式 。
插入固定点只会把一段拆成两段,删除只会把两段合成一段,改色只影响左右两段。读完操作后压缩所有可能修改的位置,用乘积线段树保存这些因子;未固定的位置因子为 。这样不需要除去旧因子,因子为 时也能正确更新。每次完成局部修改后,根结点的乘积就是答案。
预处理 ,每个路径因子在 内计算。总时间复杂度 ,空间复杂度 。
#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. 褪色圆环
颜色数最多为 ,可以直接把颜色作为转移状态。对位置 定义一个 矩阵 :
它表示从前一个位置的颜色 转移到当前位置的颜色 。设所有位置的转移矩阵按顺序相乘为 ,那么答案等于 。
展开矩阵乘法的一项,等于沿环依次选择每个位置的颜色;各位置的允许集合和各相邻位置颜色不同的条件,都在对应矩阵中检查。取对角元要求走完一圈后的颜色等于出发颜色,恰好补上首尾的相邻约束。每个合法染色对应唯一一项,故矩阵迹不重不漏地统计全部方案。
用线段树维护区间矩阵乘积,左右儿子的合并顺序必须保持为“左矩阵乘右矩阵”,不能交换。模板对应的单点矩阵可以预先生成。修改一个位置绑定的模板,只需替换一个叶子并向上更新;根矩阵的迹就是修改后的答案。
一次矩阵相乘需要 时间,建树 ,每次修改 ,空间复杂度 。实现中每个矩阵元素将至多五个乘积累加后再取模;上界小于 ,可用 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';
}
}