zoukankan      html  css  js  c++  java
  • hdu 5668 Circle 中国剩余定理

    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 finds n souls,and lets them become a circle.He ordered them to play Joseph Games.The souls will count off from the soul 1.The soul who is numbered k will be taken out,and will not join in the game again.

        Now Satiya August has got the sequence in the Out Ordered,and ask you the smallest k.If you cannot give him a correct answer,he will kill you!
     
    Input
        The first line has a number T,means testcase number.

        Each test,first line has a number n.

        The second line has n numbers,which are the sequence in the Out Ordered**(The person who is out at aith round was numbered i)**.

        The sequence input must be a permutation from 1 to n.

        1T10,2n20.
     
    Output
        For each case,If there is a eligible number k,output the smallest k,otherwise,output”Creation August is a SB!”.
     
    Sample Input
    1 7 7 6 5 4 3 2 1
     
    Sample Output
    420

     思路:先模拟将出队的顺序求出,再将同余方程求出,不互质的中国剩余定理;

       例:7 

         6 7 5 3 1 2  4

                 出队顺序:5 6 4 7 3 1 2 ;

          暴力约瑟夫得到;

           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 namespace std;
    #define ll long long
    #define mod 1000000007
    #define inf 999999999
    //#pragma comment(linker, "/STACK:102400000,102400000")
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF ) return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    ll xu[100010];
    ll pos[100010];
    ll a[100010];
    ll b[100010];
    ll flag[110];
    ll gcd(ll x,ll y)
    {
        if(x%y==0)
        return y;
        else
        return gcd(y,x%y);
    }
    void exgcd(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;
    }
    int main()
    {
        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;
            }
            int num=y;
            for(i=1;i<=y;i++)
            b[i]=num--;
            int st=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!
    ");
            else if(a1!=0)
            printf("%lld
    ",a1);
            else
            printf("%lld
    ",b1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    laravel前后端分离分页查询
    swagger-open api 手动编写规范
    linux开启端口命令
    docker容器的基本使用
    centos更换阿里云的yum源
    VS中生成pdf
    代码整洁之道阅读笔记-02
    周总结
    Mongo的基本操作
    Redis的基本操作
  • 原文地址:https://www.cnblogs.com/jhz033/p/5418669.html
Copyright © 2011-2022 走看看