AtCoder Grand Contest 038 solution to a problem

Portal

This performance is like a \ (zz \)

\(A\)

Directly before the first \ (B \) with total written \ (1 \) , then the former \ (A \) column line negated

const int N=1005;
char mp[N][N];int n,m,a,b;
int main(){
    scanf("%d%d%d%d",&n,&m,&a,&b);
    fp(i,1,b)fp(j,1,m)mp[i][j]=1;
    fp(i,1,n)fp(j,1,a)mp[i][j]^=1;
    fp(i,1,n){
        fp(j,1,m)putchar(mp[i][j]+'0');
        putchar('\n');
    }
    return 0;
}

\(B\)

Removing the first interval on the order of this case, then operation \ ([i, i + k -1] \) and \ ([i + 1, i + k] \) equivalent if and only if \ (P_i \ ) is \ ([i, i + k -1] \) minimum element and \ (p_ {i + k} \) is \ ([i + 1, i + k] \) of the largest element, then not calculated \ (i + 1 \) on the list. Stack can judge monotonous

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=2e5+5;
int a[N],st[N],up[N],dw[N],dr[N],top,n,res,k,fl;
int main(){
    scanf("%d%d",&n,&k);
    fp(i,1,n)scanf("%d",&a[i]);
    st[top=0]=0;
    fp(i,1,n){
        while(top&&a[i]>a[st[top]])--top;
        up[i]=(st[top]<=i-k),st[++top]=i;
    }
    st[top=0]=n+1;
    fd(i,n,1){
        while(top&&a[i]<a[st[top]])--top;
        dw[i]=(st[top]>=i+k),st[++top]=i;
    }
    for(R int l=1,r=2;r<=n;l=r++){
        while(r<=n&&a[r]>a[r-1])++r;
        fp(i,l+k-1,r-1)dr[i]=1;
    }
    fp(i,k,n)if(dr[i])fl=1;
        else if(!(up[i]&&dw[i-k]))++res;
    printf("%d\n",res+fl);
    return 0;
}

\(C\)

Convenience provided

\[ \begin{aligned} Ans &=\sum_{i=1}^n\sum_{j=1}^n \text{lcm}(a_i,a_j) \end{aligned} \]

Minus one minus the final answer on the line

Then start decadent persimmon

\[ \begin{aligned} Ans &=\sum_{i=1}^n\sum_{j=1}^n \text{lcm}(a_i,a_j)\\ &=\sum_{d=1}^n d\sum_{i=1}^n\sum_{j=1}^n [d|a_i][d|a_j]{a_i\over d}{a_j\over d}[\gcd({a_i\over d},{a_j\over d})=1]\\ &=\sum_{d=1}^n d\sum_{i=1}^n\sum_{j=1}^n [d|a_i][d|a_j]{a_i\over d}{a_j\over d}\sum_{t|{a_i\over d},t|{a_j\over d}}\mu(t)\\ &=\sum_{T=1}^n \sum_{d|T}d\sum_{i=1}^n\sum_{j=1}^n [T|a_i][T|a_j]{a_i\over T}{a_j\over T}\times{T^2\over d^2}\mu({T\over d})\\ &=\sum_{T=1}^n \sum_{i=1}^n\sum_{j=1}^n [T|a_i][T|a_j]{a_i\over T}{a_j\over T}T\sum_{d|T}{T\over d}\mu({T\over d})\\ &=\sum_{T=1}^n \sum_{i=1}^n\sum_{j=1}^n [T|a_i][T|a_j]{a_i\over T}{a_j\over T}T\sum_{d|T}d\mu(d)\\ \end{aligned} \]

First \ (O (n \ log n ) \) pretreated \ (S (T) = \ sum_ {D | D} T \ MU (D) \) , then write \ (cnt [k] \) represents \ ( a_i = k \) number, \ (O (n-\ log n-) \) is calculated to

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int P=998244353;
inline void upd(R int &x,R int y){(x+=y)>=P?x-=P:0;}
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
    R int res=1;
    for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
    return res;
}
const int N=1e6+5,L=1e6;
bitset<N>vis;int p[N],mu[N],cnt[N],a[N],st[N],c[N],s[N],top,n,m,res;
void init(int n=L){
    mu[1]=1;
    fp(i,2,n){
        if(!vis[i])p[++m]=i,mu[i]=-1;
        for(R int j=1;j<=m&&i*p[j]<=n;++j){
            vis[i*p[j]]=1;
            if(i%p[j]==0)break;
            mu[i*p[j]]=-mu[i];
        }
    }
    fp(i,1,n)upd(mu[i],P);
    fp(i,1,n)for(R int j=i;j<=n;j+=i)upd(s[j],mul(i,mu[i]));
}
int main(){
//  freopen("testdata.in","r",stdin);
    scanf("%d",&n);
    init();
    fp(i,1,n)scanf("%d",&a[i]),++cnt[a[i]];
    fp(T,1,L){
        R int sum=0;
        for(R int j=T;j<=L;j+=T)upd(sum,mul(j/T,cnt[j]));
        sum=mul(sum,sum),sum=mul(sum,mul(T,s[T]));
        upd(res,sum);
    }
    fp(i,1,n)res=dec(res,a[i]);
    res=mul(res,(P+1)>>1);
    printf("%d\n",res);
    return 0;
}

\(D\)

First \ (m = n-1 \ ) case determination Laid off, then only consider \ (m \ geq n \)

Build a new map \ (G \) , for all \ (C_i = 0 \) relationship \ ((a, b) \ ) as \ (a, b \) after the same communication block having such shrinkage End point for each \ (C_i = 1 \) relationship \ ((a, b) \ ) must satisfy \ (a, b \) or no solution in different communication block

Referred to as the number of communication block \ (K \) , if we remove a block from each communication point, all the analysis points connected in a ring, each communication block easily even spanning tree, so the total number of sides \ (n-\) , at least two and can ensure that any communication between different blocks point \ (2 \) paths, which is the lower bound of the number of edges, and as before we ensure that the \ (m \ geq n \) it must be able to reach the lower bound

Consider again the upper bound, even within each block spanning communication, and even the complete graph between selected points, the number of sides is \ (n-k + {k (k-1) \ over 2} = n + {k (k- 3) \ over 2} \) , then the upper bound is certainly the bigger the better, that is to say we do not need extra plus side, the direct use of \ (G \) side in order to ensure \ (k \) maximum

\ (ps: \) to the total number of connected blocks is \ (1 \) or \ (2 \) a boundary is judged are disposed on the boundary when it is not necessary to discuss

So long as the \ (m \ leq n + { k (k-3) \ over 2} \) have solutions, or no solution

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct eg{int u,v,c;}e[N];
ll m,sum;int fa[N],n,q;
inline int find(R int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
    scanf("%d%lld%d",&n,&m,&q);
    fp(i,1,n)fa[i]=i;
    for(R int i=1,u,v;i<=q;++i){
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        ++e[i].u,++e[i].v;
        if(!e[i].c)u=find(e[i].u),v=find(e[i].v),fa[u]=v;
        if(e[i].c&&m==n-1)return puts("No"),0;
    }
    if(m==n-1)return puts("Yes"),0;
    for(R int i=1,u,v;i<=q;++i)if(e[i].c){
        u=find(e[i].u),v=find(e[i].v);
        if(u==v)return puts("No"),0;
    }
    fp(i,1,n)sum+=(find(i)==i);
    sum=n-sum+(1ll*sum*(sum-1)>>1);
    puts(m<=sum?"Yes":"No");
    return 0;
}

\(E\)

First, \ (Min-Max \) accommodating conversion look-repellent, the question becomes one set for \ (S \) , provided \ (S = {T_l, T_2, .., T_m} \) , wherein the first \ (t_i \) number of occurrences reaches \ (b_ {t_i} \) the desired step count

We set up a state \ (x_1, x_2, .., x_m (x_i <t_i B_ {}) \) , represents \ (t_i \) just appeared \ (x_i \) times, provided \ (P \) generated to represent number in the set \ (S \) probabilities, the expected number of steps the state changes is \ ({. 1 \ over P} \) , the probability of the same time if calculating the state of occurrence of \ (P \) , then it contribution to answer is \ ({p \ over P} \) a. If all legal \ (x \) can calculate the contributions that add up to the set \ (S \) in the first \ (t_i \) occurrences arriving \ (b_ {t_i} \) the expected number of steps when the

For \ (P \) , provided \ (X-= \ sum_ {I ^ m} = t_i. 1 \) , there

\[ \begin{aligned} p=X!\sum_{i=1}^m\left({t_i\over P}\right)^{x_i}{1\over x_i!} \end{aligned} \]

Then you can drop set \ (f [i] [j ] [k] \) represents consideration of the \ (I \) number, which is selected from the set \ (X-J = \) , \ (\ SUM a = k \) of the total contribution for each of the last legitimate contribution to the state plus

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int P=998244353;
inline void upd(R int &x,R int y){(x+=y)>=P?x-=P:0;}
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
    R int res=1;
    for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
    return res;
}
const int N=405;
int fac[N],ifac[N],bin[N],f[N][N][N],a[N],b[N],n,s1,s2;
inline void init(R int n=400){
    fac[0]=ifac[0]=1;fp(i,1,n)fac[i]=mul(fac[i-1],i);
    ifac[n]=ksm(fac[n],P-2);fd(i,n-1,1)ifac[i]=mul(ifac[i+1],i+1);
}
int main(){
//  freopen("testdata.in","r",stdin);
    scanf("%d",&n);
    init();
    fp(i,1,n)scanf("%d%d",&a[i],&b[i]);
    f[0][0][0]=P-1;
    fp(i,1,n){
        fp(j,0,s1)fp(k,0,s2)f[i][j][k]=f[i-1][j][k];
        bin[0]=1;fp(j,1,b[i]-1)bin[j]=mul(bin[j-1],a[i]);
        fp(j,0,b[i]-1)bin[j]=mul(bin[j],ifac[j]);
        fp(j,0,s1)fp(k,0,s2)fp(v,0,b[i]-1)
            upd(f[i][j+v][k+a[i]],P-mul(f[i-1][j][k],bin[v]));
        s1+=b[i]-1,s2+=a[i];
    }
    R int res=0;
    fp(i,0,s1)fp(j,1,s2){
        R int ret=mul(f[n][i][j],fac[i]);
        ret=mul(ret,ksm(j,P-1-i));
        upd(res,mul(ret,mul(s2,ksm(j,P-2))));
    }
    printf("%d\n",res);
    return 0;
}

\(F\)

First of all, from the \ (i \) to \ (p_i \) even edge, then it is surely the formation of a number of rings, and all at the same time the number of the ring can only choose \ (i \) or \ (p_i \) , we remember selected simultaneously \ (I \) of \ (1 \) , selected simultaneously \ (P_i \) is \ (0 \)

Similarly from \ (I \) to \ (Q_I \) connected edges, but at the same time selected from the group referred to \ (I \) is \ (0 \) , selected simultaneously \ (Q_I \) of \ (1 \)

Then write \ (x [i] \) and \ (y [i] \) represent \ (I \) in which the ring, then \ (I \) whether contributes two rings are located with it values and \ (p_i \) and (q_i \) \ values related, according to \ (p_i \) and (q_i \) \ values discuss it on the line

Too lazy to write discussed directly attached solution to a problem (where \ (v_i \) represents the value of the corresponding ring)

This is obviously something can be converted into a minimum cut model

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
#define gg(u) for(int &i=cur[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=5e5+5,M=1e6+5,inf=0x3f3f3f3f;
inline int min(R int x,R int y){return x<y?x:y;}
struct eg{int v,nx,w;}e[M];int head[N],tot=1;
inline void add(R int u,R int v,R int w){
    e[++tot]={v,head[u],w},head[u]=tot;
    e[++tot]={u,head[v],0},head[v]=tot;
}
int dep[N],cur[N],S,T,n;
bool bfs(){
    memset(dep,-1,(T+1)<<2);
    memcpy(cur,head,(T+1)<<2);
    static int q[N];
    int h=1,t=0,u;q[++t]=S,dep[S]=0;
    while(h<=t){
        u=q[h++];
        go(u)if(e[i].w&&dep[v]<0)q[++t]=v,dep[v]=dep[u]+1;
    }
    return ~dep[T];
}
int dfs(int u,int lim){
    if(u==T||!lim)return lim;
    int fl=0,f;
    gg(u)if(dep[v]==dep[u]+1&&(f=dfs(v,min(lim,e[i].w)))){
        fl+=f,lim-=f,e[i].w-=f,e[i^1].w+=f;
        if(!lim)break;
    }
    if(!fl)dep[u]=-1;
    return fl;
}
inline int dinic(){R int res=0;while(bfs())res+=dfs(S,inf);return res;}
int p[N],q[N],blp[N],blq[N],s0[N],s1[N],sum,cnt;
int main(){
//  freopen("testdata.in","r",stdin);
    scanf("%d",&n);
    fp(i,1,n)scanf("%d",&p[i]),++p[i];
    fp(i,1,n)scanf("%d",&q[i]),++q[i];
    fp(i,1,n)if(!blp[i]){
        ++cnt;
        for(R int j=i;!blp[j];j=p[j])blp[j]=cnt;
    }
    fp(i,1,n)if(!blq[i]){
        ++cnt;
        for(R int j=i;!blq[j];j=q[j])blq[j]=cnt;
    }
    S=0,T=cnt+1;
    fp(i,1,n)sum+=p[i]!=q[i];
    fp(i,1,n){
        if(p[i]==q[i]){
            if(p[i]==i)continue;sum+=2;
            add(S,blp[i],1),add(blq[i],T,1),add(blp[i],blq[i],2);
        }else{
            if(p[i]==i)add(S,blq[i],1);
            else if(q[i]==i)add(blp[i],T,1);
            else add(blp[i],blq[i],1);
        }
    }
    printf("%d\n",sum-dinic());
    return 0;
}

Guess you like

Origin www.cnblogs.com/yuanquming/p/11565870.html