zoukankan      html  css  js  c++  java
  • hdu 3579 Hello Kiki

    不互质的中国剩余定理……

    链接http://acm.hdu.edu.cn/showproblem.php?pid=3579

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    int
    n,m;
    int
    extend_gcd(int a,int b,int &x,int &y)
    {

        int
    m;
        if
    (b==0)
        {

            x=1;
            y=0;
            return
    a;
        }

        else

        {

            m=extend_gcd(b,a%b,x,y);
            int
    t=x;
            x=y;
            y=t-a/b*y;
        }

        return
    m;
    }

    int
    main()
    {

        int
    t,i,j,a[7],b[7],a1,a2,b1,b2,c,tt,x,y;
        bool
    flag;
        j=0;
        cin>>t;
        while
    (t--)
        {

            cin>>n;
            for
    (i=0;i<n;i++) cin>>a[i];
            for
    (i=0;i<n;i++) cin>>b[i];
            a1=a[0];b1=b[0];flag=0;
            for
    (i=1;i<n;i++)
            {

                if
    (flag) continue;
                a2=a[i];b2=b[i];c=b2-b1;
                int
    d=extend_gcd(a1,a2,x,y);
                if
    (c%d)
                {

                    flag=true;
                    continue
    ;
                }

                tt=a2/d;
                x=(x*c/d%tt+tt)%tt;
                b1=a1*x+b1;
                a1=a1/d*a2;
            }

            if
    (flag)
            {

                printf("Case %d: -1 ",++j);
                continue
    ;
            }

            else

            {

                if
    (b1==0&&n>1) b1=a1;
                else if
    (b1==0&&n==1) b1=a[0];
                printf("Case %d: %d ",++j,b1);
            }
        }

        return
    0;
    }

  • 相关阅读:
    yum安装工具的理解
    Linux防火墙
    Python的优雅写法
    Python的time模块
    Python中根据提供的日期,返回是一年中的第几天
    观察者模式
    数据插入INSERT
    RSA加密、解密、签名、校验签名
    js的apply和call
    js插件编程-tab框
  • 原文地址:https://www.cnblogs.com/xin-hua/p/3187202.html
Copyright © 2011-2022 走看看