HDU5668 Circle non-coprime Chinese remainder theorem

Source: Internet
Author: User
Tags modulus

Analysis: Considering a simulation of a given ring sequence, we can obviously get a congruence equation by the position, number, etc.

In one lap we get the n congruence equation for each equation with extended Euclidean solution, and finally find the smallest feasible solution is the answer.

Of course, don't forget to judge the situation without solution. There are a lot of players seem to be a look at the mark and then write the hang, this is very regrettable, but this problem is quite easy to write hanging ...

Note: The time of the Chinese residual definite understanding mode linear equation Group

      There are two cases 1: one is the modulus is two coprime, such a problem can be used LRJ Petition template, commonly known as CRT1

                         2: Modulus exists coprime, such need to use the practice of merging equations, commonly known as CRT2

CRT2 can be found in this god Ben's blog (I'm looking at this, indicating a posture rise) http://972169909-qq-com.iteye.com/blog/1266328

CRT1 self-referencing LRJ petition

AC Code:

#include <stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespaceStd;typedefLong LongLL;Const intn= +;intN,c[n];BOOLVis[n]; LL A[n],m[n]; ll EXGCD (ll A, ll B, LL&x, LL &y) {    if(b = =0) {x=1, y =0; returnA; } LL D=EXGCD (b,a%b,y,x); Y-=a/b*x; returnD;} ll CRT2 (ll a[], LL m[],intnum) {    BOOLFlag =false; LL N1= m[1], N2, B1 = a[1], B2, BB, D, T, K, x, y;  for(inti =2; I <= num; i++) {n2= M[i], b2 =A[i]; BB= B2-B1; D=EXGCD (N1, N2, X, y); if(bb%d) {flag=true;  Break; } k= bb/d *x; T= n2/D; if(T <0) T =-T; K= (k% t + t)%T; B1= B1 + n1*K; N1= n1/d *N2; }    if(flag)return-1; if(B1 = =0) B1=N1; returnB1;}intMain () {intT; scanf ("%d",&T);  while(t--) {scanf ("%d",&N);  for(intI=1; i<=n; ++i) {intx; scanf ("%d",&x); C[X]=i; } memset (Vis,0,sizeof(VIS)); intnow=1;  for(intI=1; i<=n; ++i) {intCnt=0;  for(intJ=now; j<=n+1; ++j) {if(j==n+1) j=1; if(Vis[j])Continue; ++CNT; if(J==c[i]) Break; } M[i]= (n-i+1); A[i]=cnt%M[i]; Vis[c[i]]=1; if(i==n) Break;  for(intJ=c[i]; j<=n+1; ++j) {if(j==n+1) j=1; if(Vis[j])Continue; now=J;  Break; }} LL ans=CRT2 (a,m,n); if(ans==-1) printf ("Creation August is a sb!\n"); Elseprintf"%i64d\n", ans); }    return 0;}
View Code

HDU5668 Circle non-coprime Chinese remainder theorem

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.