First, we get the n congruence equation again, then we can find the smallest feasible solution by expanding Euclid, the time Complexity $o (n^2) $.
#include <cstdio> #define N 30int n,i,j,k,x,y,a[n],b[n],d[n],ans;namespace solve{int flag=1,k=1,m=0,d,x,y;int EXGCD (int a,int b,int&x,int&y) { if (!b) return x=1,y=0,a; int D=EXGCD (b,a%b,x,y), t=x; return x=y,y=t-a/b*y,d;} void Add (int a,int r) { if (!flag) return; D=EXGCD (k,a,x,y); if ((r-m)%d) {Flag=0;return;} X= (x* (r-m)/d+a/d)% (A/D), y=k/d*a,m= ((x*k+m)%y)%y; if (m<0) m+=y; K=y;} int ans () { if (!flag) return 0; return m?m:k;}} int main () { scanf ("%d", &n); for (i=1;i<=n;i++) a[i]=i,scanf ("%d", &x), b[x]=i; for (y=1,i=n;i>1;i--) { x=b[n-i+1]; Solve::add (I, ((a[x]-a[y]+1)%i+i)%i); for (d[x]=1,k=0,j=1;j<=n;j++) if (!d[j]) a[j]=++k; For (Y=x;d[j];) if ((++y) >n) Y=1; } if (Ans=solve::ans ()) printf ("%d", ans), Else puts ("NIE"); return 0;}
BZOJ2976: [Poi2002] out ring game