10.2 Zheng Rui National Day training test 1

Source: Internet
Author: User
Tags fread

Directory

    • 2018. Test
      • Summarize
      • A Chen Taiyang and taking the mold
      • B Chen Taiyang with path (tree DP)
      • C Chen Taiyang and switch
      • Exam Code
        • B
        • C
2018. Test

Time: 3.5h
Expected Score: 100+50+20
Actual score: 40+45+20

Tournament Links

Summarize

I feel like I'm a zz.

A Chen Taiyang and taking the mold

Topic links

\[\BEGIN{ALIGNED}C\%X&\EQUIV (c\%a) \%x\\c\%x&\equiv (C-\lfloor\frac ca\rfloor*a) \%x\\c\%x&\equiv c\%x- \lfloor\frac ca\rfloor*a\%x\\ (\lfloor\frac ca\rfloor*a) \%x&\equiv 0\end{aligned}\]

That is, \ (x\) should be an approximate (\lfloor\frac ca\rfloor*a\) .
\ (c\in[l,r]\), if \ (\frac la=\frac ra\) then \ (x\) can be \ (\lfloor\frac ca\rfloor\times a\) of the approximate;
Otherwise there is at least \ (\lfloor\frac la\rfloor\times a\) and \ ((\lfloor\frac la\rfloor+1) \times a\),\ (x\) It can only be an approximate of \ (a\) .
Then ask for an approximate number of numbers to be good.

//#include <cmath>#include <cstdio>#include <cctype>#include <algorithm>#define gc() getchar()typedef long long LL;inline LL read(){    LL now=0;register char c=gc();    for(;!isdigit(c);c=gc());    for(;isdigit(c);now=now*10+c-'0',c=gc());    return now;}void Div(LL x){    LL ans=1;//  for(int i=2,lim=sqrt(x); i<=lim; ++i)    for(int i=2; 1ll*i*i<=x; ++i)        if(!(x%i))        {            int cnt=1;            while(!(x%i)) x/=i, ++cnt;            ans*=cnt;        }    if(x!=1) ans<<=1ll;    printf("%lld\n",ans);}void Work(){    LL L=read(),R=read(),A=read();    if(A>R) return (void)puts("-1");    L/A==R/A ? Div(L/A*A) : Div(A);}int main(){//  freopen("ex_modulo3.in","r",stdin);    for(int T=read(); T--; Work());    return 0;}
B Chen Taiyang with path (tree DP)

Topic links

Because the height of the random tree is guaranteed, the expected maximum depth of the random tree generated by the prufer sequence is \ (o (sqrt n) \)and the average depth is \ (o (\log n) \).
So we can use two times the complexity of \ (O (N*DEP) \) the tree DP to find the answer to each point. So the spatial complexity is also \ (O (N*DEP) \) .
The useful size of a DP array is only related to depth, and can be dynamically allocated with vectors or pointers. In addition, the midpoint of the diameter is the root depth can be/2.
So the final complexity is probably \ (O (n\log n) \).

You can also use a long chain to do, for each long chain to maintain a DP array, the second time DFS only need to merge the non-long chain to the long chain.
So that any tree can do, the complexity of the (O (n) \).
--by DLs

#include <cstdio> #include <cctype> #include <algorithm>//#define GC () GetChar () #define Maxin 200000#define GC () (ss==tt&& (tt= (ss=in) +fread (In,1,maxin,stdin), SS==TT)? eof:*ss++) typedef long LONG Ll;const int n=5e5+5;int Enum,h[n],nxt[n<<1],to[n<<1],fa[n],pre[n],dis[n], Dep[n];int Pool[30000000],*now=pool,*f[n],*g[n];    LL Ans[n];char in[maxin],*ss=in,*tt=in,out[6000000],*o=out;inline int read () {int now=0;register char c=gc (); for (;!    IsDigit (c); C=GC ());    for (; IsDigit (c); now=now*10+c-' 0 ', C=GC ()); return now;}    void print (LL x) {if (x>9) print (X/10); *o++ = x%10+ ' 0 ';}    inline void AE (int u,int v) {to[++enum]=v, nxt[enum]=h[u], h[u]=enum; To[++enum]=u, Nxt[enum]=h[v], h[v]=enum;}    int BFS (int s) {static int q[n];    int h=0,t=1;    Q[0]=s, dis[s]=pre[s]=0;        while (h<t) {int x=q[h++];    for (int i=h[x],v; i; i=nxt[i]) if ((V=to[i])!=pre[x]) dis[v]=dis[x]+1, pre[v]=x, q[t++]=v; } return Q[t];}    void DFS0 (int x) {int tmp=0;    for (int i=h[x],v; i; i=nxt[i]) if ((V=to[i))!=fa[x]) fa[v]=x, DFS0 (v), Tmp=std::max (tmp,dep[v]+1);    dep[x]=tmp; F[x]=now, Now+=tmp+1, G[x]=now, now+=tmp+1;} void DFS1 (int x) {int *f=f[x];    LL res=0;            for (int i=h[x],v; i; i=nxt[i]) if ((V=to[i])!=fa[x]) {DFS1 (v); int *fv=f[v];        for (int j=0; j<=dep[v]; ++j) res+=1ll*f[j]*fv[j], f[j]+=fv[j];    } for (int i=dep[x]; i; i.) f[i]=f[i-1]; F[0]=1, Ans[x]=res;}    void DFS2 (int x) {int *g=g[x],*gf=g[fa[x]],*f=f[x],*ff=f[fa[x]];            if (x!=1) {for (int i=1; i<=dep[x]; ++i) {g[i]=gf[i-1]+ff[i-1];            if (i>=2) g[i]-=f[i-2];        Ans[x]+=1ll*g[i]*f[i]; }} for (int i=h[x],v; i; i=nxt[i]) if ((V=to[i])!=fa[x]) DFS2 (v);}    int main () {//Freopen ("Ex_diameter3.in", "R", stdin);//Freopen ("My.out", "w", stdout);    int N=read (); for (int i=1; i<n; ++i) AE (read (), ReaD ());    int S=bfs (1), RT=BFS (s), d=dis[rt]>>1;    while (DIS[RT]&GT;D) RT=PRE[RT];    DFS0 (RT), DFS1 (RT), DFS2 (RT); for (int i=1; i<n; ++i) print (ans[i]+1), *o++= ';    Print (ans[n]+1);    Fwrite (out,o-out,1,stdout); return 0;}
C Chen Taiyang and switch

Topic links

Exam Code B

Why are you running slower than ZZX? (though a penny bar)

#include <cstdio> #include <cctype> #include <algorithm>//#define GC () GetChar () #define Maxin 200000# Define GC () (ss==tt&& (tt= (ss=in) +fread (In,1,maxin,stdin), SS==TT)? eof:*ss++) typedef long LONG Ll;const int n=5e5+5;int Enum,h[n],nxt[n<<1],to[n<<1],dgr[n],f[n],numx[n], Num[n],maxd;    LL Ans[n];char in[maxin],*ss=in,*tt=in,out[6000000],*o=out;inline int read () {int now=0;register char c=gc (); for (;!    IsDigit (c); C=GC ());    for (; IsDigit (c); now=now*10+c-' 0 ', C=GC ()); return now;}    void print (LL x) {if (x>9) print (X/10); *o++ = x%10+ ' 0 ';}    inline void AE (int u,int v) {++dgr[v], to[++enum]=v, Nxt[enum]=h[u], h[u]=enum; ++dgr[u], To[++enum]=u, Nxt[enum]=h[v], h[v]=enum;    void DFS0 (int x,int fa) {int tmp=0;    for (int i=h[x],v; i; i=nxt[i]) if ((v=to[i))!=FA) DFS0 (v,x), Tmp=std::max (Tmp,f[v]); f[x]=tmp+1;} void DFS1 (int x,int fa)//{//if (dgr[x]==1) {fi[x]=0, se[x]=n; return;} int f=n,s=n;//for (int i=h[x],v; i; i=nxt[i]//if ((V=to[i])!=FA)//{//DFS1 (V,X);/if (fi[v]<f) s=f, F=fi[v], pos[x]=v;//el Se s=std::min (s,fi[v]);//}//:: F[x]=fi[x]=f+1, se[x]=s+1;//}//void DFS2 (int x,int fa)//{//for (int i=h[x],v; i; i= Nxt[i]//if ((V=to[i])!=FA)//{//if (POS[X]==V) f[v]=std::min (Se[x]+1,f[v]);/Else F[V]=STD::    Min (fi[x]+1,f[v]);//DFS2 (V,X);//}//}void DFS3 (int x,int fa,int d,int dep) {Maxd=std::max (maxd,d);    ++num[d], ++d;    if (!--dep) return; for (int i=h[x]; i; i=nxt[i]) if (TO[I]!=FA) DFS3 (TO[I],X,D,DEP);}    int main () {freopen ("ex_diameter2.in", "R", stdin);    Freopen ("My.out", "w", stdout);    int N=read ();    for (int i=1; i<n; ++i) AE (read (), read ()); DFS0 (+//DFS1), DFS2 (n/a),//for (int i=1; i<=n; ++i) printf ("f[%d]=%d fi[%d]=%d se[%d]=%d\n", I,f[i],i,fi[i],    I,se[i]);        for (int x=1; x<=n; ++x) if (dgr[x]==1) ans[x]=1; else {LL res=1; int tmp=f[x]-1; maxd=1;                for (int i=h[x]; i; i=nxt[i]) {DFS3 (to[i],x,1,tmp);            for (int j=1; j<=maxd&&num[j]; ++j) res+=1ll*numx[j]*num[j], numx[j]+=num[j], num[j]=0;            } for (int i=1; i<=tmp; ++i) numx[i]=0;        Ans[x]=res; } for (int i=1; i<n; ++i) print (Ans[i]), *o++= ';    Print (Ans[n]);    Fwrite (out,o-out,1,stdout); return 0;}
C
#include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #define GC () GetChar () #define MOD 1000000007typedef long ll;const int n=1e5+5;int N,enum,h[n],nxt[n<<1],to[n<<1];bool mp[    21][21],rev0[21],vis0[21];inline int read () {int now=0;register char c=gc (); for (;!    IsDigit (c); C=GC ());    for (; IsDigit (c); now=now*10+c-' 0 ', C=GC ()); return now;}    inline void AE (int u,int v) {to[++enum]=v, nxt[enum]=h[u], h[u]=enum; To[++enum]=u, Nxt[enum]=h[v], h[v]=enum;}    int DFS0 (int x) {int res=1; vis0[x]=1;    for (int i=1; i<=n; ++i) if (!vis0[i] && (Rev0[x]^rev0[i]^mp[x][i])) res+=dfs0 (i); return res;}    BOOL Check () {memset (vis0,0,sizeof vis0); return DFS0 (1) ==n;}    void Subtask1 () {memset (mp,0,sizeof MP);    for (int x=1, x<=n; ++x) for (int i=h[x]; i; i=nxt[i]) mp[x][to[i]]=1;    int ans=1,all= (1<<n)-1; for (int s=1, s<=all; ++s) {for (int i=0; i<n; ++i) if (s>>i&1) rev0[I+1]=1;        Ans+=check ();    for (int i=0; i<n; ++i) if (s>>i&1) rev0[i+1]=0; } printf ("%d\n", ans);}    void work () {enum=0, memset (h,0,sizeof H);    N=read ();    for (int i=1; i<n; ++i) AE (read (), read ());        if (n<=20) {Subtask1 (); return;}    }int Main () {//Freopen ("Ex_trigger1.in", "R", stdin);//Freopen (". Out", "w", stdout); for (int t=read (); t--;    Work ()); return 0;}

10.2 Zheng Rui National Day training test 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.