跳到正文

题解

题解:P9252 [PA 2022] Wielki Zderzacz Termionów

DYYqwq约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年8月31日
收录
2026年8月31日
更新
2026年11月12日
篇幅
约 7 分钟
题目
P9252

版本

  1. v1当前

提供一种单位根做法。感谢 @CYFqwq 对我的悉心指导,没有他,我根本就不可能会这个做法。

容易注意到设 C 为 11,Z 为 22,只有和在 mod⁡3\operatorname{mod}3 意义下不为 00 才可能是合法的。同时,我们也要求最终序列不能是奇数长度 CZ 交替的。

考虑毫无限制,那么答案为 2m2^{m},这里 mm 表示 N 的个数。考虑扣去使得答案不合法的 N 的贡献。

这里有一个细节,可以现设所有 N 都是 11,实际上的方案是选择部分数字 +1+1。

比如,当前余数就是 00,如果你的 N 做的贡献还是 00,就不合法。这种情况下,相当于求:

∑i=0m[imod⁡3=0]Cmi\sum_{i=0}^{m}[i \operatorname{mod}3=0]C_{m}^{i}

也就是,我们要求多项式:

F(x)=∑i=0mCmixiF(x)=\sum_{i=0}^{m}C_{m}^{i}x^i

的某部分系数和。

先说结论:以余 00 为例,答案就是 F(ω32)+F(ω3)+F(1)3\frac{F(\omega_3^2) + F(\omega_3)+F(1)}{3}。其中 ω3\omega_3 为三次单位根。

分析之前,明确一个引理:ω32+ω3+1=0\omega_3^2+\omega_3+1=0,这个读者自证不难。

简要分析:

  1. i≡0i\equiv0 时,三项贡献全为 11。
  2. i≡1i\equiv1 时,对于同一个组合数,三者贡献分别为 ω32,ω3,1\omega_3^2,\omega_3,1,由引理,相加为 00,等同于不贡献。
  3. i≡2i\equiv2 时,对于同一个组合数,三者贡献分别为 ω3,ω32,1\omega_3,\omega_3^2,1,由引理,相加为 00,等同于不贡献。

综上,这就是答案。

具体怎么求,运用二项式定理即可,如 F(ω32)=(ω32+1)mF(\omega_3^2)=(\omega_3^2+1)^m。

对于其他余数情况,我们发现无非就是把多项式 ii 头上的系数多了 11 或 22,在外面乘上即可。

但是现在产生了极其严峻的问题:我们遍历了 22 到 109+610^9+6,试图找到 mod⁡109+7\operatorname{mod}10^9+7 意义下的三次单位根,但是居然没有输出??

于是考虑扩域。将所有数表示为 aω+ba\omega+b 的形式,就像虚数一样。然后乘法的定义是简单的:(aω3+b)(cω3+d)=acω32+(bc+ad)ω3+bd(a\omega_3+b)(c\omega_3+d)=ac\omega_3^2+(bc+ad)\omega_3+bd,由引理,上式即为 ac(−ω3−1)+(bc+ad)ω3+bdac(-\omega_3-1)+(bc+ad)\omega_3+bd,剩下的自己化吧。

最终输出的答案 aω3+ba\omega_3+b,考虑其数学意义,其中的 aa 一定为 00,所以不必担惊受怕,输出实数部分即可。

写代码的时候铭记引理内容,遇到 ω32\omega_3^2 化成 −ω3−1-\omega_3-1 即可。

可以参考代码。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int n , q , cnt[3][2] , m;
const int inv3 = 333333336;
inline int id(char c)
{
    if(c == 'C') return 1;
    if(c == 'Z') return 2;
    if(c == 'N') return 0;
    __builtin_unreachable(); assert(false);
}
struct node
{
	int a , b;
	node() {a = 0 , b = 0;}
	node(int x) {a = 0 , b = x;}
	node(int x , int y) {a = x , b = y;}
	node operator + (const node &qwq) const {return (node){(a + qwq.a) % mod , (b + qwq.b) % mod};}
	node operator - (const node &qwq) const {return (node){(a - qwq.a + mod) % mod , (b - qwq.b + mod) % mod};}
	node operator * (const node &qwq) const
	{
		int c = qwq.a , d = qwq.b;
		node now;
		now.a = (b * c % mod + a * d % mod - a * c % mod + mod) % mod;
		now.b = (b * d % mod - a * c % mod + mod) % mod;
		return now;
	}
};
node fast_mi(node x , int y)
{
	node qwq = x , ans = {0 , 1};
	while(y)
	{
		if(y & 1) ans = ans * qwq;
		y >>= 1 , qwq = qwq * qwq;
	}
	return ans;
}
inline node calc(node x , int y)
{
	// (x+1)^m * (x^y)
	return fast_mi(x + (node){0 , 1} , m) * fast_mi(x , y); 
}
int solve()
{
	int ans = fast_mi({0 , 2} , m).b;
	if(n == 1) return ans;
	int sum = (cnt[1][0] + cnt[1][1]) + (cnt[2][0] + cnt[2][1]) * 2 + m;
	sum %= 3;
	ans -= (calc({mod - 1 , mod - 1} , sum) + calc({1 , 0} , sum) + calc({0 , 1} , sum)).b * inv3 % mod;
    if(n & 1) ans -= (cnt[1][0] == 0 && cnt[2][1] == 0) + (cnt[1][1] == 0 && cnt[2][0] == 0);
    return (ans + mod) % mod;
}
signed main()
{
	scanf("%lld%lld" , &n , &q); string s; cin >> s; s = '_' + s;
    for(int i = 1 ; i <= n ; i ++)
	{
        cnt[id(s[i])][i & 1] ++;
		m += (s[i] == 'N');
    }
    printf("%lld\n" , solve());
    while(q --)
	{
        int pos; char c; scanf("%lld" , &pos); cin >> c;
        int old = id(s[pos]);
        int nw = id(c);
        cnt[old][pos & 1] -- , m -= (old == 0);
        s[pos] = c;
        cnt[nw][pos & 1] ++ , m += (nw == 0);
		printf("%lld\n" , solve());
    }
    return 0;
}
原文
存档评论1 条抓取于