HDU 5668 Circle China remainder theorem

Source: Internet
Author: User

Circle

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)


Problem Description    Satiya August is in charge of souls.

    He findsNSouls,and lets them become a circle. He ordered them to play Joseph games.the souls would count off from the soul1. The soul is numberedkWould be taken Out,and won't join in the game again.

Now Satiya August have got the sequence in the out Ordered,and ask you the smallest k. If You cannot give him a correct answer,he would kill you!

Input    The first line has a number T,means testcase number.

    Each test,first line has a numberN.

    The second line hasNNumbers,which is the sequence in the ordered**aitH Round was numberedI)**.

    The sequence input must is a permutation from1ToN.

1≤T≤2≤n≤ .

Outputfor each case,if there is a eligible number k, output the Smallest k, Otherwise,output "Creation August is a sb!".

Sample Input177 6 5 4 3 2 1

Sample Output420

Idea: First simulation will be out of the order of the team to find out, and then the same residual equation, not coprime Chinese residual theorem;

Example: 7

6 7 5 3 1 2 4

Squad Order: 5 6 4 7 3 1 2;

Violent Joseph get;

5≡k mod (n);

1≡k mod (n-1);

5≡k mod (n-2);

1≡k mod (n-3);

3≡k mod (n-4);

1≡k mod (n-5);

1≡k mod (n-6);

#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell Long Long#defineMoD 1000000007#defineINF 999999999//#pragma COMMENT (linker, "/stack:102400000,102400000")intScan () {intres =0, ch;  while( ! (ch = getchar ()) >='0'&& CH <='9' ) )    {        if(ch = = EOF)return 1<< - ; } Res= CH-'0' ;  while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;} ll xu[100010];ll pos[100010];ll a[100010];ll b[100010];ll flag[ the];ll gcd (ll x,ll y) {if(x%y==0)    returny; Else    returnGCD (y,x%y);}voidEXGCD (ll A, ll B, ll &x, LL &y) {    if(b = =0) {x=1; Y=0; return; } EXGCD (b, a%b, x, y); LL TMP=x; X=y; Y= tmp-(A/b) *y;}intMain () {ll x,y,z,i,t; scanf ("%lld",&x);  while(x--) {memset (A,0,sizeof(a)); memset (Flag,0,sizeof(flag)); scanf ("%lld",&y);  for(i=0; i<y;i++) {scanf ("%lld",&Xu[i]); Pos[xu[i]]=i+1; }        intnum=y;  for(i=1; i<=y;i++) B[i]=num--; intst=1;  for(i=1; i<=y;i++)        {             while(1)            {                if(st==Pos[i]) Break; if(!flag[st]) a[i]++; St++; if(st==y+1) St=1; } A[i]++; FLAG[ST]=1; //cout<<i<< "" <<a[i]<<endl;} ll A1=a[1],b1=b[1]; ll Jie=1;  for(i=2; i<=y;i++) {ll A2=a[i],b2=B[i];            ll Xx,yy; ll Gys=gcd (B1,B2); if((A2-A1)%Gys) {Jie=0;  Break;            } exgcd (B1,B2,XX,YY); XX= (xx* (A2-A1))/Gys; LL GBS=b1*b2/Gys; A1= (((XX*B1+A1)%gbs) +gbs)%GBS; B1=GBS; }        if(!Jie) printf ("Creation August is a sb!\n"); Else if(a1!=0) printf ("%lld\n", A1); Elseprintf ("%lld\n", B1); }    return 0;}
View Code

HDU 5668 Circle China 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.