Chinese remainder theorem.

You can manually simulate the number of people who start each time and the number of people who end it.

Remove one person at a time and renumber the remainder.

By simulating this one, we can get n equations.

Shape: (u[i]+k)% (n-i+1) =v[i]

Simplification is: k% (n-i+1) =v[i]-u[i]% (n-i+1)

The next is to solve the smallest k, which satisfies all the equations

China surplus theorem board a set of AC ...

#include <cstdio>#include<cstring>#include<cmath>#include<queue>#include<algorithm>using namespaceStd;typedefLong LongLL;Const intMAXN = -;intN, T;intS[MAXN], FLAG[MAXN], TMP[MAXN];intu[ -], v[ -]; LL A[MAXN], B[MAXN];voidls () {intCNT =1; for(inti =1; I <= N; i++) { if(Flag[i] = =0) tmp[cnt++] =i; }}intFind (inta) { for(inti = A; I <= N; i++) if(Flag[i] = =0)returni; for(inti =1; I <= A; i++) if(Flag[i] = =0)returni;}intF2 (inta) { for(inti =1;; i++) if(Tmp[i] = = a)returni;}voidEGCD (ll A, ll B, Ll&d, Ll&x, ll&y) { if(!B) {d = a, x =1, y =0; } Else{EGCD (b, a%B, D, y, x); Y-= x* (A/b); }}ll LMEs () {LL M= a[1], R = b[1], x, Y, D; for(inti =2; I <= N; i++) {EGCD (M, A[i], D, X, y); if((B[i]-R)% d)return-1; X= (B[i]-R)/d*x% (A[i]/BD); R+ = x*l; M= m/d*A[i]; R%=M; } return(R + m)% m? (R + M)%m:m;}voidEXGCD (ll A, ll B, LL &d, LL &x, LL &y) { if(b = =0) d= A, x =1, y =0; Else{EXGCD (b, a%B, D, y, x); Y-= x * (A/b); }}ll China (ll N, ll m[], LL a[]) {ll AA= a[0]; LL mm= m[0]; for(inti =0; i<n; i++) {LL Sub= (A[i]-AA); LL d, x, y; EXGCD (MM, m[i], D, X, y); if(Sub% d)return-1; LL new_m= M[i]/D; New_m= (sub/d*x%new_m + new_m)%new_m; AA= Mm*new_m +AA; MM= Mm*m[i]/D; } AA= (aa + mm)%mm; returnAa?aa:mm;}intMain () {scanf ("%d", &T); while(t--) {scanf ("%d", &N); for(inti =1; I <= N; i++) { intAi scanf"%d", &AI); S[ai]=i; } memset (Flag,0,sizeofflag); u[1] =0, v[1] = s[1]; if(v[1] = = N) v[1] =0; flag[s[1]] =1; for(inti =2; I <= N; i++) { intSt, en; LS (); St= Find (S[i-1]), st = F2 (st), st = St-1; if(St = =-1) st = n-i; En= F2 (S[i]), u[i] =St; V[i]= en;if(V[i] = = n-i+1) V[i] =0; Flag[s[i]]=1; } /*for (int i = 1; I <= n; i++) printf ("%d%d\n", U[i], v[i]); */ //the next is to solve the smallest k, which satisfies all the equations//k% (n-i+1) =v[i]-u[i]% (n-i+1) for(inti =1; I <= N; i++) {A[i]= (LL) (N-i +1); B[i]= (LL) (V[i]-u[i]% (N-i +1)); } LL k= China (n, A +1, B +1); if(k = =-1) printf ("Creation August is a sb!\n"); Elseprintf"%lld\n", K); } return 0;}

HDU 5668 Circle