A. 【线上训练13】二叉树
题解:贪心,先求出这两个序列,i在第一个序列位置为,第二个为,如果,
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const int mo = 998244353;
int n,tot,cnt;
int l[maxn],r[maxn],p[maxn],q[maxn],tmp[maxn];
int vis[maxn],ans1[maxn],ans2[maxn];
ll ans;
void dfs1(int x){
p[++cnt] = x;
if(l[x])
dfs1(l[x]);
if(r[x])
dfs1(r[x]);
}
void dfs2(int x){
if(l[x])
dfs2(l[x]);
if(r[x])
dfs2(r[x]);
q[++tot] = x;
}
ll qp(ll a,ll b){
ll ans = 1,base = a;
while(b != 0){
if(b & 1)ans = ans * base % mo;
base = base * base % mo;
b >>= 1;
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i ++){
scanf("%d%d",&l[i],&r[i]);
}
dfs1(1);
dfs2(1);
for(int i = 1;i <= n;i ++){
if(vis[p[i]] == 0 && vis[q[i]] == 0){
vis[p[i]] = vis[q[i]] = 1;
if(p[i] != q[i])tmp[p[i]] = 1,tmp[q[i]] = 0;
else tmp[p[i]] = tmp[q[i]] = 0;
ans1[i] = 1;
ans2[i] = 0;
}
if(vis[p[i]] && vis[q[i]]){
ans1[i] = tmp[p[i]];
ans2[i] = tmp[q[i]];
}
if(vis[p[i]] && vis[q[i]] == 0){
ans1[i] = tmp[p[i]];
ans2[i] = 0;
tmp[q[i]] = 0;vis[q[i]] = 1;
}
if(vis[p[i]] == 0 && vis[q[i]]){
ans2[i] = tmp[q[i]];
ans1[i] = 1;
tmp[p[i]] = 1;vis[p[i]] = 1;
}
}
ll sum1 = 0,sum2 = 0;
for(int i = 1;i <= n;i ++){
if(ans1[i])sum1 = (sum1 + ans1[i] * qp(2,n - i)) %mo;
if(ans2[i])sum2 = (sum2 + ans2[i] * qp(2,n - i)) %mo;
}
ans = (sum1 - sum2 + mo)%mo;
cout<<ans<<endl;
return 0;
}
B. 【线上训练13】子序列
题解:子序列,表示到i的子序列个数,转移见代码。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 5010100;
const int mo = 998244353;
int m,len;
char c[maxn],s[maxn<<1],tmp[maxn];
int last[30];
ll dp[maxn];
ll ans;
int main()
{
scanf("%d",&m);
scanf("%s",c + 1);
len = 0;
for(int i = 1;i <= m;i ++){
len *= 2;
for(int j = 1;j <= len;j ++){
if(j % 2 == 1)tmp[j] = c[i];
else tmp[j] = s[j/2];
}
tmp[++len] = c[i];
for(int j = 1;j <= len;j ++)s[j] = tmp[j];
}
for(int i = 1;i <= len;i ++){
if(last[s[i] - 'a'] == 0)dp[i] = (dp[i-1]*2% mo + 1)% mo;
else dp[i] = (dp[i - 1] * 2 % mo - dp[last[s[i] - 'a'] - 1] + mo) %mo;
last[s[i] - 'a'] = i;
// cout<<dp[i]<<endl;
}
cout<<dp[len]<<endl;
return 0;
}T3
咕咕咕