题解:P9252 [PA 2022] Wielki Zderzacz Termionów
提供一种单位根做法。感谢 @CYFqwq 对我的悉心指导,没有他,我根本就不可能会这个做法。
容易注意到设 C 为 Z 为 CZ 交替的。
考虑毫无限制,那么答案为 N 的个数。考虑扣去使得答案不合法的 N 的贡献。
这里有一个细节,可以现设所有 N 都是
比如,当前余数就是 N 做的贡献还是
也就是,我们要求多项式:
的某部分系数和。
先说结论:以余
分析之前,明确一个引理:
简要分析:
综上,这就是答案。
具体怎么求,运用二项式定理即可,如
对于其他余数情况,我们发现无非就是把多项式
但是现在产生了极其严峻的问题:我们遍历了
于是考虑扩域。将所有数表示为
最终输出的答案
写代码的时候铭记引理内容,遇到
可以参考代码。
#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;
}