Strange Way to Express Integers扩展欧几里德

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include <math.h>
#include<string.h>
#include <algorithm>
#define INF 1000000000
using namespace std;
//typedef long long LL;
long long ggcd(long long a,long long b,long long&x,long long&y)
{
    long long d,t;
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    d=ggcd(b,a%b,x,y);
    t=x-a/b*y;
    x=y;
    y=t;
     return d;

}
int main()
{
    long long  a1,r1,a2,r2,a,b,c,x0,y0,m,t;
    int i,n;
    while(scanf("%d",&n)!=EOF)
    {
        int bj=1;
        scanf("%lld%lld",&a1,&r1);
        for(i=1;i<n;i++)
        {
            scanf("%lld%lld",&a2,&r2);
            a=a1;
            b=a2;
            c=r2-r1;
            m=ggcd(a,b,x0,y0);
            if(c%m)
                bj=0;
            t=b/m;
            x0*=(c/m);
            x0=(x0%t+t)%t;
            r1=a1*x0+r1;
            a1=a1*(a2/m);

        }
        if(bj==0)
        {
            printf("-1\n");
        }
        else
            printf("%lld\n",r1);
    }
    return 0;
}

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

从后往前推有一个固定的式子;x=1;y=0;....

x=y;y=x-a/b*y;

    
时间: 2024-10-12 09:05:18

Strange Way to Express Integers扩展欧几里德的相关文章

POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 互质就简单,但是不互质就有些麻烦,到现在我还是不大懂. 具体证明可以求教大佬,如果我懂了,会更新的. 代码 #include <cstring> #include <cstdio> #include <algorithm> #in

poj 2891 Strange Way to Express Integers (扩展gcd)

题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2联立得:a1*x-a2*y=r2-r1;设r=r2-r2; 互质的模线性方程组m=r[i](mod a[i]).两个方程可以合并为一个,新的a1为lcm(a1,a2), 新的r为关于当前两个方程的解m,然后再和下一个方程合并--.(r2-r1)不能被gcd(a1,a2)整除时无解. 怎么推出的看了好

POJ 2891-Strange Way to Express Integers(扩展欧几里德)

题目地址:POJ 2891 题意:给你k组同余关系,每组包含一个ai和ri,让你找出一个最小的数m,满足m%a1=r1,m%a2=r2.......m%ak=rk. 思路:纵观上述公式,很熟悉,其实就是求两两公式之间的最小值,例如K=3,那么先求第一组和第二组的最小,然后合并第一组和第二组,然后用合并之后的再和第三组找最小,最后的结果就是最终的结果.也就是这个题分两部分来完成. 1.找出两组最小.对于m%a1=r1和m%a2=r2可以得出两个公式m=a1*x+r1,m=a2*y+r2(x,y相当

POJ.2891.Strange Way to Express Integers(扩展CRT)

题目链接 扩展中国剩余定理:1(直观的).2(详细证明). #include <cstdio> #include <cctype> #define gc() getchar() typedef long long LL; const int N=1e6+5; LL n,m[N],r[N]; inline LL read() { LL now=0,f=1;register char c=gc(); for(;!isdigit(c);c=gc()) if(c=='-') f=-1; f

【POJ 2891】Strange Way to Express Integers(扩展欧几里得)

[POJ 2891]Strange Way to Express Integers(扩展欧几里得) Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 12934   Accepted: 4130 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative in

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

Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2891 Appoint description:  System Crawler  (2015-04-27) Description Elina is reading a book written by Rujia Liu,

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

http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 11970   Accepted: 3788 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express no

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

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.

解题报告 之 POJ2891 Strange Way to Express Integers

解题报告 之 POJ2891 Strange Way to Express Integers 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,