zoukankan      html  css  js  c++  java
  • poj 2891(中国剩余定理)

    Strange Way to Express Integers
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 13056   Accepted: 4167

    Description

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

    Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ ik) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.

    “It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

    Since Elina is new to programming, this problem is too difficult for her. Can you help her?

    Input

    The input contains multiple test cases. Each test cases consists of some lines.

    • Line 1: Contains the integer k.
    • Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ ik).

    Output

    Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

    Sample Input

    2
    8 7
    11 9

    Sample Output

    31

    Hint

    All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

    直接用kuangbin大神的模板了,话说kuangbin大神的模板真好用,,无论模的数是不是互质都可以用。。

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    const int N = 1000;
    LL extend_gcd(LL a,LL b,LL &x,LL &y)
    {
        if(b==0)
        {
            x=1,y=0;
            return a;
        }
        else
        {
            LL x1,y1;
            LL d = extend_gcd(b,a%b,x1,y1);
            x = y1;
            y = x1-a/b*y1;
            return d;
        }
    }
    LL m[N],a[N];///模数为m,余数为a, X % m = a
    bool solve(LL &m0,LL &a0,LL m,LL a)
    {
        long long y,x;
        LL g = extend_gcd(m0,m,x,y);
        LL t = a-a0>0?a-a0:a0-a;
        if( t%g )return false;
        x *= (a - a0)/g;
        x %= m/g;
        a0 = (x*m0 + a0);
        m0 *= m/g;
        a0 %= m0;
        if( a0 < 0 )a0 += m0;
        return true;
    }
    /**
    * 无解返回false,有解返回true;
    * 解的形式最后为 a0 + m0 * t (0<=a0<m0)
    */
    bool MLES(LL &m0 ,LL &a0,LL n)///解为 X = a0 + m0 * k
    {
        bool flag = true;
        m0 = 1;
        a0 = 0;
        for(int i = 0; i < n; i++)
            if( !solve(m0,a0,m[i],a[i]) )
            {
                flag = false;
                break;
            }
        return flag;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for(int i=0; i<n; i++)
            {
                scanf("%lld%lld",&m[i],&a[i]);
            }
            LL m0,a0;
            bool flag = MLES(m0,a0,n);
            if(!flag) printf("-1
    ");
            else
            {
                LL x = (a0%m0+m0)%m0;
                printf("%lld
    ",x);
            }
        }
        return 0;
    }
  • 相关阅读:
    自定义View
    Android Parcelable
    java IO
    如何安全退出已调用多个Activity的Application?
    cookie和session
    Excel 使用AutoFill提示“类Range的AutoFill方法无效”
    解决“配置系统未能初始化”问题
    Android控件第7类——对话框
    Android控件第6类——杂项控件
    Android控件第5类——ViewAnimator
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5533949.html
Copyright © 2011-2022 走看看