POJ 2891 Strange Way to Express Integers(擴充套件GCD)

畫船聽雨發表於2014-03-04

題意就是給你k組數,每組是m,a.有一個數字x對每組的m取餘得到的餘數為a。讓你求出滿足條件的最小的x。

分析假設只有一組資料那最小的就是餘數a。如果有兩組的話:x%m1 = a1,x%m2 = a2.  m1*y+a1 = m2*z+a2。化簡得到:m1*y-m2*z=a2-a1.通過擴充套件GCD可以得到一組解y,z。所以可以得到一個x = y*m1+a1。滿足條件。如果x=(y*m1+a1)+k*LCM(m1,m2)(m1,m2的最小公倍數).可以滿足所有的解集。所以這兩個公式可以合併為一個公式:T%LCM(m1,m2) = y*m1+a1.這裡的T是x=(y*m1+a1)+k*LCM(m1,m2)中的某一個數字。

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

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 a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (1 ≤ i ≤ k).

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 101000;

using namespace std;

LL x, y;

LL exit_gcd(LL a, LL b)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    LL p = exit_gcd(b, a%b);
    LL t = x;
    x = y;
    y = t-a/b*y;
    return p;
}

LL find_x(LL a, LL b, LL m)
{
    LL d;
    d = exit_gcd(a, m);
    if(b%d)
        return -1;
    return ((x*(b/d)%m)+m)%m;
}

int main()
{
    int k;
    LL m1, m2, a1, a2;
    LL y;
    while(cin >>k)
    {
        cin >>m1>>a1;
        int flag = 0;
        k--;
        while(k--)
        {
            cin >>m2>>a2;
            if(flag)
                continue;
            y = find_x(m2, a1-a2, m1);
            if(y == -1)
            {
                flag = 1;
                continue;
            }
            m1 = m1*m2/__gcd(m1,m2);//求出最小公倍數
            a1 = a2+m2*y;//算出之前滿足條件的x,即為現在的餘數
            a1 = (a1%m1+m1)%m1;//防止有越界的可能
        }
        if(flag)
            cout<<"-1"<<endl;
        else
            cout<<a1<<endl;
    }
    return 0;
}


相關文章