A. XOR
To find out the XOR and $sum$ of all the numbers, $sum$ all the numbers and and then the linear basis, then selects all $1$ corresponding base optimality of $sum$.
Time Complexity $o (N\log x) $.
#include <cstdio> #include <cstdlib> #include <algorithm> #include <ctime>using namespace std; typedef long LONG ll;int case,n,i,j;ll ans,sum,a,b,a[100],x,v[111111],pool[100];const int n=20;ll cal () {ll ans=sum;for ( int s=0; s<1<<n; s++) {ll a=sum,b=0;for (i=0;i<n;i++) if (s>>i&1) a^=v[i],b^=v[i]; A-=b;if (a<0) a=-a;if (A<ans) ans=a;} return ans;} void print (ll x) {for (int i=11;~i;i--) printf ("%lld", x>>i&1);p UTS ("");} ll Myabs (ll x) {return x>0?x:-x;} ll solve1 (int x) {ll cur=a[x];for (int i=x-1;~i;i--) if (sum>>i&1) {cur^=a[i];} Return Myabs (cur-(Sum^cur));} ll solve2 (int x) {ll cur=0;for (int i=x-1;~i;i--) {Cur=max (cur,cur^a[i]);} Return Myabs (cur-(Sum^cur));} int main () {//srand (Time (NULL)), scanf ("%d", &case), while (case--) {scanf ("%d", &n);//n=rand ()%10+1;sum=0;for (i=0;i<61;i++) a[i]=0;for (i=0;i<n;i++) {scanf ("%lld", &x);//x=rand ()%1000;v[i]=x;sum^=x;for (j=60;~j;j-- if (x>>j&1) {if (A[j]) x^=a[j];else {a[j]=x;break;}}} ll Vio=cal (); for (i=0;i<61;i++) for (j=i+1;j<61;j++) if (a[j]>>i&1) a[j]^=a[i];for (i=60;~i;i--) if (sum>>i &1) Break;int lim=i;if (lim>=0) {for (i=0;i<61;i++) pool[i]=a[i]&sum,a[i]=0;for (i=0;i<61;i++) {x= Pool[i];for (j=60;~j;j--) if (x>>j&1) {if (A[j]) x^=a[j];else {a[j]=x;break;}}} for (i=0;i<61;i++) for (j=i+1;j<61;j++) if (a[j]>>i&1) a[j]^=a[i];ans=min (Sum,min (Solve1 (Lim), solve2 (Lim)));} Else{ans=0;} /*a=sum,b=0;*///print (sum);//for (i=60;~i;i--) if (A[i]) print (a[i]);/*ans=sum;for (i=60;~i;i--) if ((A^a[i]) >= (B^ A[i]) && (A^a[i])-(b^a[i]) <ans) {a^=a[i]; B^=a[i];ans=a-b;} *//*if (Ans==vio) puts ("OK"), else{printf ("Vio=%lld ans=%lld\n", Vio,ans);p rintf ("%d\n", N); for (i=0;i<n;i++) printf ("%lld", V[i]);p UTS (""); while (1);} */printf ("%lld\n", ans);}}
B. Tribute
Press test instructions to simulate.
#include <bits/stdc++.h>using namespace Std;int casenum, casei;typedef unsigned int ui;int n;vector<ui>vt, Wt;multiset<ui>sot;multiset<ui>ans;bool solve () {for (int i = 1; I <= n; ++i) {int x = *sot.begin (); ans.inser t (x);//printf ("x =%d\n", x),//for (auto y:vt) printf ("%d", y); Puts (""); Wt.clear (); for (auto y:vt) {int z = x + y;//printf ("%d\n", z), if (Sot.find (z) = = Sot.end ()) return 0;sot.erase (Sot. Find (z)); Wt.push_back (z);} for (auto y:wt) vt.push_back (y);} return 1;} int main () {scanf ("%d", &casenum), for (casei = 1; casei <= casenum; casei + +) {scanf ("%d", &n); int m = (1 << N)-1;vt.clear (); Vt.push_back (0); Sot.clear (); Ans.clear (); for (int i = 1; I <= m; ++i) {int x; scanf ("%d", &x); Sot.insert (x);} if (!solve ()) puts ("NO"), Else{int id = 0;for (auto It:ans) printf ("%d%c", it, ++id = = n?) ' \ n ': ');}}}
C. Boardroom meeting
CDQ Division + Scan Line tree array, time complexity $o (N\LOG^2N) $.
#include <cstdio> #include <algorithm>using namespace Std;const int N=200010;int Case,n,i,a[n],b[n],c[n], Ans,f[n],qa[n],qb[n],bit[n],vis[n],t;inline void Up (Int&a,int b) {a<b? ( A=B): 0;} inline void ins (int x,int p) {for (; x<=n;x+=x&-x) if (vis[x]<t) vis[x]=t,bit[x]=p;else up (bit[x],p);} inline void Ask (int x,int&p) {for (; x;x-=x&-x) if (vis[x]==t) up (P,bit[x]);} inline BOOL cmp (int x,int y) {return a[x]<a[y];} void Solve (int l,int r) {if (l==r) {up (ans,++f[l]); return;} int mid= (L+R) >>1,i,j,ca=0,cb=0;solve (L,mid); for (i=l;i<=mid;i++) qa[++ca]=i;for (; i<=r;i++) qb[++cb]=i; Sort (qa+1,qa+ca+1,cmp); sort (qb+1,qb+cb+1,cmp); T++;for (i=j=1;i<=cb;i++) {while (J<=ca&&a[qa[j]]<a[qb[i]]) {INS (b[qa[j]],f[qa[j]]); j + +;} Ask (B[qb[i]]-1,f[qb[i]);} Solve (mid+1,r);} int main () {scanf ("%d", &case), while (case--) {scanf ("%d", &n), for (i=1;i<=n;i++) scanf ("%d", &a[i]); (i=1;i<=n;i++) scanf ("%d", &b[i]), C[i]=b[i];sort (c+1,c+n+1); for (i=1;i<=n;i++) B[i]=lower_bound (C+1,c+n+1,b[i])-c;for (i=1;i<=n;i++) f[i]=0;ans=0;solve (1,n);p rintf ("%d\n", ans);}} /*161 2 6 3 4 64 1 3 5 7 7*/
D. Secret Santa
The second class of Stirling number tolerance, time complexity $o (a\log N) $.
#include <cstdio>const int n=2010,p=1000000007;int n,a,i,j,case,fac[n],s[n][n];int po (int a,int b) {int t=1;for (; b;b>>=1,a=1ll*a*a%p) if (b&1) T=1ll*t*a%p;return t;} int T (int m,int n) { int ans=0; for (int k=0;k<m;k++) { int t=fac[k]; if ((k+m)%2) t= (p-t)%P; T=1ll*t*po (k+1,n)%P; t=1ll*t*s[m][k+2]%p; Ans= (ans+t)%P; } return ans;} int main () {for (i=0;i<n;i++) for (j=0;j<n;j++) { if (i>=1&&j==0) {s[i][j]=0;continue;} if (i==j) {s[i][j]=1;continue;} if (j>=1&&j<i) s[i][j]= (1ll*s[i-1][j]*j+s[i-1][j-1])%P; } for (fac[0]=i=1;i<n;i++) fac[i]=1ll*fac[i-1]*i%p; scanf ("%d", &case); while (case--) { scanf ("%d%d", &n,&a); a++;n-=a-2; printf ("%d\n", T (A,n));} }
E. Guessing Game
Pressure DP, set $f[s]$ represents the best strategy in the case of $s$ the worst required steps, wherein $s$ is a $k$ bit $3$ binary number, respectively, each one is not asked, asked and answered $0$, asked and answered $1$.
With a high-dimensional prefix and preprocessing of all $f[s]=0$ states, the remaining State enumeration asks which one to transfer.
Time complexity $o (3^KK) $.
#include <cstdio> #include <algorithm>using namespace Std;const int N=13,m=1600000;int p[20],case,n,m,i,j, K,f[m],c[m];char s[20];inline int get (int x,int y) {return x/p[y]%3;} int main () {for (p[0]=i=1;i<20;i++) p[i]=p[i-1]*3;scanf ("%d", &case) and while (case--) {scanf ("%d%d",&n,& k); M=p[k];for (i=0;i<m;i++) c[i]=0;for (i=1;i<=n;i++) {scanf ("%s", s); int t=0;for (j=0;j<k;j++) t=t*3+s[j]-' 0 '; c[t]++;} for (i=0;i<k;i++) for (j=0;j<m;j++) if (Get (j,i) ==2) {c[j]+=c[j-p[i]]+c[j-p[i]*2];} for (i=0;i<m;i++) {if (c[i]<=1) {f[i]=0;continue;} F[i]=m;for (j=0;j<k;j++) if (Get (i,j) ==2) {f[i]=min (F[i],max (f[i-p[j]],f[i-p[j]*2]));} f[i]++;} printf ("%d\n", F[m-1]);}}
F. Flat Earth
Press test instructions to simulate.
#include <stdio.h> #include <math.h>int casenum, casei, a[10];const double PI = ACOs ( -1.0); int main () {scanf (" %d ", &casenum); for (casei = 1; casei <= casenum; casei + +) {for (int i = 1; I <= 8; i + +) {scanf ("%d ", &a[i]);} printf ("%.10f\n", PI * a[4] * a[4]);}}
G. We need more managers!
Create a graph of $2^n$ points, each of which represents each string. For each point, the $n$ point is changed to a single-bit edge.
Then the test instructions can be converted to a minimum spanning tree of 22 shortest-circuiting as a weighted value for a given $m$ point.
Set $p_x$ and $d_x$ respectively to indicate the closest to each point of the given string and corresponding distance, can be found in BFS, then for a side $ (u,v) $, in the spanning tree to add a connection $p_u$ and $p_v$, the weight is $d_u+d_v+1$ edge.
Time Complexity $o (N2^n\alpha (m)) $.
#include <cstdio> #include <algorithm>using namespace Std;const int n=1100000;int case,n,m,all,i,j,a[n]; Char s[100];int h,t,d[n],p[n],x,y,z,q[n];int f[n],ans,ce;struct e{int x, y, Z; E () {}e (int _x,int _y,int _z) {x=_x,y=_y,z=_z;}} e[n*20];inline BOOL CMP (const E&a,const e&b) {return a.z<b.z;} inline void ext (int x,int y,int z) {if (y<d[x]) {d[x]=y;p[x]=z;q[++t]=x;}} int F (int x) {return f[x]==x?x:f[x]=f (f[x]);} inline void Add (int x,int y,int z) {if (x!=y) e[++ce]=e (x, y, z);} int main () {scanf ("%d", &case), while (case--) {scanf ("%d%d", &n,&m); All=1<<n;for (i=0;i<all;i++) D[i]=n;for (i=1;i<=m;i++) {scanf ("%s", s), A[i]=0;for (j=0;j<n;j++) a[i]=a[i]*2+ (s[j]== ' L ');d [a[i]]=0,p[a[i]]= i;} H=1,t=0;for (i=0;i<all;i++) if (!d[i]) q[++t]=i;while (h<=t) {x=q[h++];y=d[x]+1;z=p[x];for (i=0;i<n;i++) Ext ( x^ (1<<i), y,z);} Ce=0;for (i=0;i<all;i++) for (j=0;j<n;j++) Add (p[i],p[i^ (1<<j)],d[i]+d[i^ (1<<j)]+1), for (i=1;i <=m;i++) F[i]=i;ans=0;sort (E+1,E+CE+1,CMP); for (I=1; i<=ce;i++) if (F (e[i].x)!=f (E[I].Y)) f[f[e[i].x]]=f[e[i].y],ans+=e[i].z;printf ("%d\n", ans);}}
H. Masterpiece
If each point is not considered to have been traversed by the first few times, then the scheme is unique, $O (n) $ simulation After the calculation of the number of bifurcation points $t$, the answer is $2^t$.
#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue > #include <bitset> #include <algorithm> #include <time.h>using namespace std;void fre () {} #define MS (x, y) memset (x, y, sizeof (x)) #define RT 1,1,n#define ls o<<1#define rs o<<1|1#define mid (l+r>>1) #def Ine Lson ls,l,mid#define Rson rs,mid+1,rtypedef Long long ll;typedef unsigned long long ul;typedef unsigned int ui;templat E <class T1, class t2>inline void Gmax (T1 &a, T2 b) {if (b > a) a = b;} Template <class T1, class t2>inline void Gmin (T1 &a, T2 b) {if (b < a) A = b;} const int N = 1e5 +, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;template <class T1, class t2>inline void Gadd (T1 &a , T2 b) {a = (A + b)% Z;} int Casenum, Casei;int n;int r[n], c[n];//try go to (y, x) bool flag = 1;int R1, R2;int C1, C2;bool doit (int y, int x) {if (r1 = = R2 && C1 = = C2) return flag = 1; if (Y > N | | x > N) return 0; if (!r[y] | |! c[x]) return 0; --r[y]; --C[X]; return flag = 1;} int solve () {int ans = 1; R1 = 1, C1 = 1; r2 = 1, c2 = 1; Doit (1, 1); --R[1]; --C[1]; while (1) {flag = 0; if (r1 = = R2 && C1 = = C2) {if (r1 = = N && C1 = = N) {return ans ; } int rnum = R[R1]; int cnum = C[C1]; if (rnum && cnum) {ans = ans * 2 Z; for (int i = 1; I <= rnum; ++i) {if (!doit (R1, ++C1)) return 0; } for (int i = 1; I <= cnum; ++i) {if (!doit (++R2, C2)) return 0; }} else if (rnum) {if (!doit (R1, ++C1)) return 0; ++C2; } else if (cnum) {if (!doit (++R1, C1)) return 0; ++R2; } else return 0; } while (R1 < R2) {if (R[r1]) {if (!doit (R1, ++C1)) return 0; } else if (!doit (++R1, C1)) return 0; } while (R2 < R1) {if (R[r2]) {if (!doit (R2, ++C2)) return 0; } else if (!doit (++R2, C2)) return 0; } while (C1 < C2) {if (C[c1]) {if (!doit (++R1, C1)) return 0; } else if (!doit (R1, ++C1)) return 0; } while (C2 < C1) {if (C[c2]) {if (!doit (++R2, C2)) return 0; } else if (!doit (R2, ++C2)) return 0; } if (!flag) return 0; }}int Main () {scanf ("%d", &casenum); for (casei = 1; Casei <= Casenum; ++casei) {scanf ("%d", &n); int n = rand ()% 10 + 1; for (int i = 1; I <= n; ++i) {scanf ("%d", &r[i]); R[i] = rand ()% (n + 1); } for (int i = 1; I <= n; ++i) {scanf ("%d", &c[i]); C[i] = rand ()% (n + 1); } int ans = solve (); for (int i = 1; I <= n; ++i) {if (R[i] | | c[i]) ans = 0; } printf ("%d\n", ans);} return 0;} /* "trick&&" "Test Instructions" "Analysis" "Time Complexity && Optimization" 10053 3 3 3 31 4 4 3 344 2 2 44 2 2 4*/
I. Don ' t Split the atom!
The outcome depends only on the parity of the $n$.
#include <stdio.h> #include <math.h>int casenum, casei, a[10];const double PI = ACOs ( -1.0); int main () {scanf (" %d ", &casenum); for (casei = 1; casei <= casenum; casei + +) {int n;scanf ("%d ", &n);p UTS (N & 1?) "B": "A");}}
J. Bobby Tables
The number of combinations can be considered as an interval of length $k$ divided by a prefix of length $k$.
Enumeration of each $k$ as a prefix, the interval corresponding to the two, take the logarithmic acceleration determination, in the vicinity of the model to determine the match.
Time Complexity $o (m\log m) $.
#include <cstdio> #include <cmath> #include <algorithm>using namespace std;typedef long double ld; const int N=200010,p=1000000007,k=20;const ld eps=1e-2;int case,n,m,i,a[n],fac[n],inv[n],mul;ld Log[N],s[N],sum; inline bool Check (int n,int m) {ld a=s[n]-s[n-m]-s[m];if (fabs (a-sum) >eps) return 0;int b=1ll*fac[n]*inv[n-m]%p*inv[m ]%p;if (B!=mul) return 0;puts ("YES");p rintf ("%d%d\n", n,m); return 1;} inline void Solve () {scanf ("%d%d", &n,&m), Sum=0;mul=1;for (i=1;i<=n;i++) {scanf ("%d", &a[i]); mul=1ll* Mul*a[i]%p;sum+=log[a[i]];} for (i=1;i<=m;i++) {//[x,x+i-1]/[1..i]//c (x+i-1,i)//x+i-1>=i//1<=x<=m+1-i//x+i-1<=mint l=1,r=m+1-i while (l<=r) {int mid= (l+r) >>1;ld a=s[mid+i-1]-s[mid-1]-s[i];if (Fabs (a-sum) <eps) {for (int J=max (mid-K,1 ); J<=min (m+1-i,mid+k); j + +) if (check (j+i-1,i)) Return;break;} if (a<sum) L=mid+1;else r=mid-1;}} Puts ("NO");} int main () {for (i=1;i<n;i++) log[i]=log2 (i), for (i=1;i<n;i++) s[i]=s[i-1]+log[i];for (inv[0]=inv[1]=1,i=2;i <n;i++) Inv[i]=1ll* (p-inv[p%i]) * (p/i)%p;for (fac[0]=i=1;i<n;i++) fac[i]=1ll*fac[i-1]*i%p;for (i=1;i<n;i++) INV[I]=1LL*INV [I-1]*inv[i]%p;scanf ("%d", &case), while (case--) {Solve ();}}
K. Triples
Long-chain or tree-divided classical problems.
#include <cstdio> #include <vector> #include <algorithm> #include <cstring> #define PB push_back #define V vector#define N 200010using namespace Std;typedef long long ll;typedef pair<int,ll>p;int case;int n,i,x,y, G[n],v[n<<1],nxt[n<<1],ed,f[n],del[n];ll ans; v<p>h[n];inline void Add (int x,int y) {v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} inline BOOL CMP (V<INT>*A,V<INT>*B) {return a->size () >b->size ();} Template<class c>inline C&get (v<c>&a,size_t x) {return a[a.size ()-x-1];} V<int>*dfs (int x,int y) {v<v<int>*>t; for (int i=g[x];i;i=nxt[i]) if (v[i]!=y) T.PB (DFS (v[i],x)); if (!t.size ()) return new v<int> (.); Sort (T.begin (), T.end (), CMP); v<int>*a=t[0]; A->PB (1); if (T.size () ==1) return A; v<ll>b; B.resize (T[1]->size () +1,0); for (int i=1;i<t.size (); i++) {v<int>*u=t[i]; for (int j=1;j<=u->size (); j + +) {int Ch=get (*u,j-1), &rt=get (*A,J); Ll&rd=get (B,J); Ans+=1ll*ch*rd; Rd+=1ll*ch*rt; Rt+=ch; }} for (int i=1;i<b.size (); i++) {H[X].PB (P (I,get (b,i))); Ans-=1ll*get (b,i) *get (*a,i); } return A; void cal (int x,int y) {f[x]=1; for (int i=g[x];i;i=nxt[i]) if (V[i]!=y&&!del[v[i]) cal (V[i],x), F[x]+=f[v[i]];} inline int findroot (int x) {cal (x,-1); int n=f[x],t=0,y=-1; do{t=0; for (int i=g[x];i;i=nxt[i]) if (v[i]!=y&&!del[v[i]]&&2*f[v[i]]>n) {y=x,x=v[i],t=1; Break }}while (t); return x;} inline void work (v<v<int> >&d,V<V<P> >&q,int k,bool rt,int x) {int st=k==1?0:d.size ()-1 , En=k==1?d.size ():-1; V<int>a (1,RT); for (int i=st;i!=en;i+=k) {v<int>d=d[i]; For (V<p>::iterator p=q[i].begin ();p!=q[i].end ();p + +) {int j=p->first; if (J<a.size ()) ans+=1ll*a[j]*p->second; } a.resize (Max (A.size (), D.size ()), 0); for (int j=0;j<d.size (); j + +) A[j]+=d[j]; } if (RT) for (V<p>::iterator P=h[x].begin ();p
L. related Languages
Enumeration $o (NM) $ to the right end of the interval, the value of the left endpoint satisfies monotonicity, the double pointer can be.
Time complexity $o (NM) $.
#include <cstdio>const int n=4010;int case,n,m,i,j,k,ans,g[n*3];char a[n],b[n],f[n][n];int w[N*3],V;int main () { scanf ("%d", &case); while (case--) { scanf ("%d%d%d%s%s", &n,&m,&v,a+1,b+1); ans=0; for (i=1;i<=n;i++) for (j=1;j<=m;j++) f[i][j]=a[i]!=b[j]; for (i=0;i<=n+n+5;i++) g[i]=w[i]=0; for (i=1;i<=n;i++) for (j=1;j<=m;j++) { k=i-j+n; g[k]++; W[K]+=F[I][J]; int g=g[k],w=w[k]; while (g>0&&w>v) { g--; W-=F[I-G][J-G]; } g[k]=g; W[k]=w; if (G>ans) ans=g; } printf ("%d\n", ans);} }
Petrozavodsk Winter-2018. Jagiellonian U Contest