# AGC038 部分题解

## E

### 题意

$\forall i \in [0,n-1]$ $i$ 被生成至少 $B_i$ 次时停止生成，求期望生成次数，对大质数取模。

### 题解

$\sum_{i \geq 0} P[X=i]i = \sum_{i \geq 0} P[X \geq i]$

$\sum_{S \subseteq [n],S \neq \emptyset} (-1)^{|S|-1}\frac{\sum_{i=1}^n A_i}{\sum_{i \in S} A_i} \sum_{T=0}^{\sum_{i \in S} (b_i-1) + 1} \sum_{\{b\},\sum_{i \in S} b_i = T} \prod_{i \in T} (\frac{A_i}{\sum_{i \in S} A_i})^{b_i}\frac{T!}{\prod_{i \in S} b_i!}$

$\sum_{S \subseteq [n],S \neq \emptyset} (-1)^{|S|-1} \frac{\sum_{i=1}^n A_i}{\sum_{i \in S} A_i} \sum_{T=0}^{\sum_{i \in S} (b_i-1) + 1} T!(\frac{1}{\sum_{i \in S} A_i})^T \sum_{\{b\},\sum_{i \in S} b_i = T} \prod_{i \in S} \frac{A_i^{b_i}}{b_i!}$

$f_{i,j,k} = f_{i-1,j,k}-\sum_{x=0}^{B_i-1}\frac{A_i^x}{x!} f_{i-1,j-A_i,k-x}$

\begin{aligned} \sum_{i=1}^n \sum_{j=0}^{\sum A_i} \sum_{k=0}^{\sum B_i } \sum_{l=0}^{B_i-1} 1 &= \sum_{i=1}^n \sum_{l=0}^{B_i-1} (\sum A_i) (\sum B_i)\\ &=(\sum A_i)(\sum B_i)^2 \\ &= O(n^3) \end{aligned}

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 400+5;
const int ha = 998244353;
int n,a[MAXN],b[MAXN];
int f[MAXN][MAXN];
int sma,smb;

x += y-ha;x += x>>31&ha;
}

inline int qpow(int a,int n=ha-2){
int res = 1;
while(n){
if(n & 1) res = 1ll*res*a%ha;
a = 1ll*a*a%ha;
n >>= 1;
}
return res;
}

int fac[MAXN],inv[MAXN];

int main(){
fac[0] = 1;FOR(i,1,MAXN-1) fac[i] = 1ll*fac[i-1]*i%ha;
inv[MAXN-1] = qpow(fac[MAXN-1]);ROF(i,MAXN-2,0) inv[i] = 1ll*inv[i+1]*(i+1)%ha;
scanf("%d",&n);
FOR(i,1,n) scanf("%d%d",a+i,b+i);
FOR(i,1,n) sma += a[i],smb += b[i]-1;
f[0][0] = -1;
FOR(i,1,n){
ROF(j,sma,a[i]){
ROF(k,smb,0){
ROF(x,std::min(k,b[i]-1),0){
}
}
}
}
int ans = 0;
FOR(j,0,sma) FOR(k,0,smb){
int gx = f[j][k];
gx = 1ll*gx*fac[k]%ha*sma%ha;
gx = 1ll*gx*qpow(qpow(j,k+1))%ha;
}
printf("%d\n",ans);
return 0;
}


## F

### 题意

• $A_i$ 取值 $\{i,P_i\}$
• $B_i$ 取值 $\{i,Q_i\}$

### 题解

$P,Q$ 分成若干个环，同一个环要么都取 $i$ 要么都取 $P_i/Q_i$

• $P_i=Q_i=i$：无论如何都相等。
• $P_i,Q_i \neq i,P_i \neq Q_i$：同时不转相等。
• $P_i=i,Q_i \neq i$$B_i$ 不转相等。
• $P_i \neq i,Q_i = i$$A_i$ 不转相等。
• $P_i=Q_i,P_i,Q_i \neq i$：同时转相等。

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 2e5 + 5;

struct Edge{
int to,w,nxt;
}e[MAXN<<4];

inline void add(int u,int v,int w){
}

int S,T,N;

inline bool bfs(){
FOR(i,0,N) cur[i] = head[i],dep[i] = 0;
std::queue<int> q;q.push(S);dep[S] = 1;
while(!q.empty()){
int v = q.front();q.pop();
for(int i = head[v];i;i = e[i].nxt){
if(e[i].w > 0 && !dep[e[i].to]){
dep[e[i].to] = dep[v]+1;
q.push(e[i].to);
}
}
}
return dep[T];
}

inline int dfs(int v,int lim=1e9){
if(v == T) return lim;
if(!lim) return 0;
int ans = 0;
for(int &i = cur[v];i;i = e[i].nxt){
if(e[i].w > 0 && dep[e[i].to] == dep[v]+1){
int t = dfs(e[i].to,std::min(lim,e[i].w));
if(t > 0){
ans += t;
e[i].w -= t;
e[i^1].w += t;
lim -= t;
if(!lim) break;
}
}
}
return ans;
}

inline int Dinic(){
int ans = 0,flow;
while(bfs()) while((flow=dfs(S))) ans += flow;
return ans;
}

int n,p[MAXN],q[MAXN];
bool vis[MAXN];
int belp[MAXN],belq[MAXN];

inline void dfs(int v,int now,int p[],int bel[]){
vis[v] = 1;bel[v] = now;
if(vis[p[v]]) return;
dfs(p[v],now,p,bel);
}

int main(){
scanf("%d",&n);
FOR(i,1,n) scanf("%d",p+i),++p[i];
FOR(i,1,n) scanf("%d",q+i),++q[i];
FOR(i,1,n) if(!vis[i]) ++N,dfs(i,N,p,belp);
FOR(i,1,n) vis[i] = 0;
FOR(i,1,n) if(!vis[i]) ++N,dfs(i,N,q,belq);
S = N+1;T = N+2;N = T;
int ans = n;
FOR(i,1,n){
if(p[i] == q[i]){
if(p[i] == i){
ans--;continue;
}
else{
}
}
else{
if(p[i] == i && q[i] != i){
}
else if(p[i] != i && q[i] == i){
}
else{
}
}
}
printf("%d\n",ans-Dinic());
return 0;
}
/*
p: 割左边=不转 右边=转
q反过来

p[i]=q[i]=i 无论如何都会-1
p[i],q[i] != i 同时不转
p[i]=i,q[i]!=i 2不转
p[i]!=i,q[i]=i 1不转
p[i]=q[i],p[i],q[i]!=i  同时转
*/


posted @ 2020-11-20 15:38  RainAir  阅读(13)  评论(0编辑  收藏