Strange Way to Express Integers (poj 2891)

原创 2018年04月16日 17:23:57
Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 18386 Accepted: 6199

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

num%a1=r1

num%a2=r2

......

num%an=rn

像中国剩余定理,但是r1,r2......rn并不互质。那就直接解同余方程组。

先解方程一:可得num=c+mt;

代入方程二:可得c+mt+y*a2=r2,移项:mt+y*a2=r2,解t=c1+m1t,num2=c+m*(c1+m1t);

类推,解出最后一个num;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define INF 1005

using namespace std;

long long k;
long long a[INF],r[INF];//分别表示an与rn 

long long extgcd(long long a,long long b,long long& x,long long& y)
{
    long long d=a;
    if(b!=0){
        d=extgcd(b,a%b,y,x);
        y-=(a/b)*x;
    }
    else{
        x=1,y=0;
    }
    return d;
}
long long linear_congruence(int n)
{
    long long c=0,m=1;
    for(int i=0;i<n;i++){
        long long ai=m,bi=r[i]-c;
        long long x,y;
        long long g=extgcd(ai,a[i],x,y);
        if(bi%g!=0) return -1;
        long long m1=a[i]/g;
        c=((x*bi/g)%m1+m1)%m1*m+c;
        m=m1*m;
    }
    return c;
}

int main()
{
    while(scanf("%lld",&k)!=EOF){
        for(int i=0;i<k;i++){
            scanf("%d%d",&a[i],&r[i]);
        }
        long long num=linear_congruence(k);
        cout<<num<<endl;
    }
}
/*将方程一般化为aix%biy=ri;
数组设a[n],b[n],r[n];*/


long long linear_congruence(int n)
{
    long long c=0,m=1;
    for(int i=0;i<n;i++){
        long long ai=m*a[i],ri=r[i]-c*a[i];
        long long x,y;
        long long g=extgcd(ai,b[i],x,y);
        if(ri%g!=0) return -1;
        long long m1=b[i]/g;
        c=((x*ri/g)%m1+m1)%m1*m+c;
        m=m1*m;
    }
    return c;
}

poj 2891 Strange Way to Express Integers 中国剩余定理

Strange Way to Express IntegersTime Limit: 1000MS Memory Limit: 131072KTotal Submissions: 4191 Accep...
  • hqd_acm
  • hqd_acm
  • 2011-02-07 15:46:00
  • 2101

poj 2891 Strange Way to Express Integers (解模线性方程组)

题意:有一个数x,给定k组ai和ri,使得x%ai=ri 求x最小为多少 分析:求解模线性方程组   x = a1(mod m1)   x = a2(mod m2)   x = a3(mo...
  • acm_code
  • acm_code
  • 2014-11-05 15:16:05
  • 746

poj2891--Strange Way to Express Integers(不互素的中国剩余定理)

题目链接:点击打开链接 题目大意:有一个数x,x%ai = ri ,给出n对ai和ri,问x的最小非负整数是什么,如果不存在输出-1 不互素的中国剩余定理: x%a1= r1 ; x%a2 = ...
  • u013015642
  • u013015642
  • 2015-08-05 09:34:50
  • 582

POJ 2891 Strange Way to Express Integers(exgcd—解一元线性同余方程组)

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Sub...
  • zwj1452267376
  • zwj1452267376
  • 2016-08-19 20:13:04
  • 319

poj 2891 Strange Way To Express Integers(线性同余方程组)

Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, wh...
  • u012717411
  • u012717411
  • 2015-01-22 11:11:01
  • 966

POJ-2891 Strange Way to Express Integers (数论:扩展欧几米德定理)

POJ-2891 Strange Way to Express Integers(扩展欧几米德定理)题意: 已知 k组数据ai,ri,求满足 x≡ri(mod ai)的最小值x min...
  • no_alternantive
  • no_alternantive
  • 2016-08-04 16:58:32
  • 360

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:...
  • u013015642
  • u013015642
  • 2014-08-07 21:44:26
  • 1536

Strange Way to Express Integers(中国剩余定理+不互质)

Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I6...
  • u010579068
  • u010579068
  • 2015-05-03 00:26:05
  • 1634

POJ 2891解题报告

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: ...
  • u012294939
  • u012294939
  • 2015-03-03 16:47:27
  • 442

2891 Strange Way to Express Integers

Strange Way to Express IntegersTime Limit: 1000MS Memory Limit: 131072KTotal Submissions: 3301 Accep...
  • hqd_acm
  • hqd_acm
  • 2010-08-04 09:59:00
  • 566
收藏助手
不良信息举报
您举报文章:Strange Way to Express Integers (poj 2891)
举报原因:
原因补充:

(最多只允许输入30个字)