正睿停课集训4

A

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#include<cmath>
#include<set>
#include<map>
#include<assert.h>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std;
const int N = 5e5 + 3;
struct edge{
int to;
int nxt;
}e[N << 1];
int L[N],R[N],s[N],cnt;
int n,m,tot;
int ans[N],fa[N],t;
bool flag[N];
e[++tot].to = y;
}
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline void dfs(int x,int f,int dep){
if(dep & 1) printf("%d ",x);
for(int i = head[x];i;i = e[i].nxt){
int y = e[i].to;
if(y == f) continue;
dfs(y,x,dep + 1);
}
if(!(dep & 1)) printf("%d ",x);
}
int main(){
puts("Yes");
for(int i = 1;i < n;++i){
}
dfs(1,0,1);
return 0;
}


B

$|T| \le |S| \le 2\times 10^5 ,m \le 10$

$f_{i} = \min f_j + a_i$

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#include<cmath>
#include<set>
#include<map>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std;
const int N = 3e5 + 3;
const int INF = 2e9 + 7;
char s[N];
int a[N],f[N];
char t[11][N];
int nxt[11][N];
int len[11];
int n,m;
vector <pii> B,G;
int Pi[N];
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline void kmp(int x){
nxt[x][1] = 0;
int j = 0;
for(int i = 2;i <= len[x];++i){
while(j && t[x][j + 1] != t[x][i]) j = nxt[x][j];
if(t[x][j + 1] == t[x][i]) j++;
nxt[x][i] = j;
}
j = 0;
for(int i = 1;i <= n;++i){
while(j && t[x][j + 1] != s[i]) j = nxt[x][j];
if(t[x][j + 1] == s[i]) j++;
if(j == len[x]){
G.push_back(mk(i - len[x] + 1,i));
j = nxt[x][j];
}
}
}
inline bool cmp(pii x,pii y){
return x.se < y.se || (x.se == y.se && x.fi > y.fi);
}
struct BIT{
int c[N];
inline void pre(){
for(int i = 1;i <= n + 1;++i) c[i] = INF;
}
inline void ins(int x,int v){
c[x] = min(c[x],v);
for(;x <= n + 1;x += x & (-x)) c[x] = min(c[x],v);
}
inline int query(int x){
int res = INF;
for(;x;x -= x & (-x)) res = min(res,c[x]);
return res;
}
}T;
int main(){
scanf("%s",s + 1);
for(int i = 1;i <= n;++i) a[i] = read();
for(int i = 1;i <= m;++i){
scanf("%s",t[i] + 1);
len[i] = strlen(t[i] + 1);
kmp(i);
}
sort(G.begin(),G.end(),cmp);
for(int i = 0;i < G.size();++i){
if(B.empty()){
B.push_back(G[i]);
Pi[G[i].se] = G[i].fi;
continue;
}
else{
if(G[i].fi <= B.back().fi && G[i].se >= B.back().se) continue;
else B.push_back(G[i]);
Pi[G[i].se] = G[i].fi;
}
}
//	for(int i = 1;i <= n;++i) printf("%d ",Pi[i]);puts("");
T.pre();
f[0] = 0;T.ins(n + 1,0);
int now = 0;int la = 0;
for(int i = 1;i <= n;++i){
f[i] = T.query(n - la + 1) + a[i];
//		printf("%d %d\n",i,T.query(n - la + 1));
T.ins(n - i + 1,f[i]);
la = max(la,Pi[i]);
while(now < Pi[i]) f[now] = INF,now++;
}
//	for(int i = 0;i <= n;++i) printf("%d ",f[i]);puts("");
int ans = INF;
for(int i = 0;i <= n;++i) ans = min(ans,f[i]);
printf("%d\n",ans);
return 0;
}


C

$f_{i,j} = f_{i,j } + f_{i - 1,j - 1} \times size_k (k\in son_i)$

inline void ins(int x,int sz){
for(int i = d[x] - 1;i >= 0;--i)
dp[x][i + 1] = mo1(dp[x][i + 1] + 1ll * dp[x][i] * sz % mod);
}
inline void del(int x,int sz){
for(int i = 0;i < d[x];++i){
dp[x][i + 1] = mo2(dp[x][i + 1] - 1ll * dp[x][i] * sz % mod);
}
}


$\sum_{i = 0}^k \binom{k}{i}\times dp_{u,k - i}\times (k -i)!$

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#include<cmath>
#include<set>
#include<map>
#include<assert.h>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std;
const int N = 4e5 + 3;
const LL mod = 998244353;
struct edge{
int to;
int nxt;
}e[N << 1];
int fac[N],inv[N];
int dp[N][505];
int fa[21][N],d[N];
int n,m,L,tot;
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline LL quick(LL x,LL y){
LL res = 1;
while(y){
if(y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
e[++tot].to = y;
d[y]++;
}
inline void dfs(int x,int f,int dep){
deep[x] = dep;
fa[0][x] = f;
size[x] = 1;
for(int i = head[x];i;i = e[i].nxt){
int y = e[i].to;
if(y == f) continue;
dfs(y,x,dep + 1);
size[x] += size[y];
}
}
inline int mo1(int x){
if(x >= mod) x -= mod;
return x;
}
inline int mo2(int x){
if(x < 0) x += mod;
return x;
}
inline void ins(int x,int sz){
for(int i = d[x] - 1;i >= 0;--i)
dp[x][i + 1] = mo1(dp[x][i + 1] + 1ll * dp[x][i] * sz % mod);
}
inline void del(int x,int sz){
for(int i = 0;i < d[x];++i){
dp[x][i + 1] = mo2(dp[x][i + 1] - 1ll * dp[x][i] * sz % mod);
}
}
inline int LCA(int x,int y){
if(deep[x] < deep[y]) swap(x,y);
for(int i = 19;i >= 0;--i)
if(deep[fa[i][x]] >= deep[y]) x = fa[i][x];
if(x == y) return x;
for(int i = 19;i >= 0;--i)
if(fa[i][x] != fa[i][y]) x = fa[i][x],y = fa[i][y];
return fa[0][x];
}
inline int kfa(int x,int k){
for(int i = 19;i >= 0;--i) if(k & (1 << i)) x = fa[i][x];
return x;
}
inline int C(int n,int m){
return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
bool flag = 0;
int main(){
//	freopen("C.in","r",stdin);
//	freopen("C.out","w",stdout);
for(int i = 1;i < n;++i){
}
fac[0] = inv[0] = 1;
for(int i = 1;i <= L;++i) fac[i] = 1ll * fac[i - 1] * i % mod;
inv[L] = quick(fac[L],mod - 2);
for(int i = L - 1;i >= 1;--i) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
dfs(1,0,1);
//for(int i = 1;i <= n;++i) printf("%d ",size[i]);puts("");
for(int j = 1;j < 20;++j){
for(int i = 1;i <= n;++i)
fa[j][i] = fa[j - 1][fa[j - 1][i]];
}
for(int i = 1;i <= n;++i){
dp[i][0] = 1;
for(int j = head[i];j;j = e[j].nxt){
int y = e[j].to;
int sz = (y == fa[0][i] ? n - size[i] : size[y]);
ins(i,sz);
}
}
while(m--){
int lca = LCA(x,y),sz1,sz2;
//	printf("%d\n",lca);
if(deep[x] > deep[y]) swap(x,y);
sz1 = (x == lca ? size[kfa(y,deep[y] - deep[x] - 1)] : n - size[x]);
sz2 = n - size[y];
del(x,sz1),del(y,sz2);
int ans1 = 0,ans2 = 0;
for(int i = 0;i <= k;++i){
ans1 = mo1(ans1 + 1ll * C(k,i) * dp[x][k - i] % mod * fac[k - i] % mod);
ans2 = mo1(ans2 + 1ll * C(k,i) * dp[y][k - i] % mod * fac[k - i] % mod);
}
printf("%lld\n",1ll * ans1 * ans2 % mod);
ins(x,sz1),ins(y,sz2);
}
return 0;
}


posted @ 2019-10-22 20:19  wyxdrqcccc  阅读(49)  评论(0编辑  收藏