BZOJ2976: [Poi2002] out ring game

Source: Internet
Author: User

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

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.