POJ 2891 Strange Way to Express Integers(扩展GCD)

来源:互联网 发布:python 移动文件 编辑:程序博客网 时间:2024/06/06 07:07

题意就是给你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: 131072KTotal 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

28 711 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.1415926535898const 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;}


0 0
原创粉丝点击