POJ2891-Strange Way to Express Integers(同余方程求解)

9 篇文章 0 订阅
1 篇文章 0 订阅

Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 17853 Accepted: 6013
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 ≤ i ≤ k) 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 ≤ 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
Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
题目:POJ2891
题意:给出一个数n,对于一些数取模后的余数,求n的最小值。
思路:中国剩余定理,对于两个同余方程:
r1≡n(mod a1)
r2≡n(mod a2)
将其展开:
a1*x+r1=n
a2*y+r2=n
联立移项并且令c=r2-r1,得:a1*x-a2*y=c
我们得到了一个二元一次方程,如果求出这个方程的通解,并且找到一个最小的x(x>=0)由a1*x+r1=n,我们可以得到一个最小的n,同时,这两个同余方程就可以合并成一个,如果有多个方程,我们至于要一一合并就可以得到最终的答案。
我们知道拓展欧几里德算法可以求出方程:ax+by=gcd(a,b)的通解。
那么,我们可以求出a1*x-a2*y=gcd(a1,a2)的通解。
如果c%gcd(a1,a2)==0,那么原方程是由整数解的,否则无整数解
通过拓展欧几里德算法,我们可以表示方程的通解为:
X=X0+a2/gcd
Y=Y0-a1/gcd
那么x=x%(a2/gcd)即是满足条件最小的x
新方程的r1=a1*x+r1,a1=a1/gcd*a2(两数的最小公倍数)。
如此一直迭代下去,就可以把所有式子合并成一个。
对于最终的方程:a*x+r=n,当x=0时,n最小。
AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
ll a1,a2,r1,r2,x,y;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int gcd=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return gcd;
}
ll congruence(ll n/*方程个数*/)
{
    int flag=0;
    ll divisor1,divisor2,remain1,remain2;
    scannl(divisor1,remain1);
    for (int i = 1; i <n; ++i)
    {
        scannl(divisor2,remain2);
        ll remain=remain2-remain1;
        ll gcd=exgcd(divisor1,divisor2,x,y);
        if(remain%gcd!=0)flag=1;//无解
        ll t=divisor2/gcd;
        x=(x*(remain/gcd)%t+t)%t;
        remain1=divisor1*x+remain1;//新方程的余数
        divisor1=divisor2/gcd*divisor1;//新方程的除数
    }
    if(flag)return -1;
    return  remain1;//当x=0时,可以取得最小值,即为remain1
}
int main()
{
    ll n;
    while(~scanl(n))
    {
        prinl(congruence(n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值