题解:CF2069C Beautiful Sequence
题意
定义一个“美丽”的序列为:除第
题解
我们看到只会有
我们还得想如何把它变成 1 2 3 2 1 3 2 2 3。对于每个
现在我们需要人类智慧。我们发现后面减的数就是这个
- 遇到
3 就cnt \leftarrow cnt + 1,now \leftarrow now + 2^0 。 - 遇到
1 就把答案增加now - cnt 。 - 遇到
2 就now \leftarrow now \times 2 。
这样我们就成功的搞出了一个
代码
#include<bits/extc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int n;
void solve()
{
cin >> n;
vector<int>a(n + 5);
for (int i = 1; i <= n; i++)
cin >> a[i];
int cnt = 0,ans = 0,now = 0;
for (int i = n; i >= 1; i--)
{//从后往前循环,模拟即可
if (a[i] == 3)
{
cnt++;
now++;
}
else if (a[i] == 1)
ans = (ans + now - cnt + mod) % mod;
else
now = (now << 1) % mod;
}
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}