您的位置:首页 > 其它

poj 2891 Strange Way to Express Integers

2016-10-10 13:52 239 查看
Strange Way to Express Integers

Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 14514 Accepted: 4748
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.

Source
POJ Monthly--2006.07.30, Static

题意:
求解线性同余方程组
solution:
基本是模板题。。。对于线性同余方程组,如果模数两两互质,直接上中国剩余定理,然而此题并不行--
对于一般情形,是可以两两合并的
考虑x ≡ r1 (mod a1) 与 x ≡ r2 (mod a2)
解一定是以x = k1*a1 + r1 = k2*a2 + r2存在的
两边同时mod a2,整理得
x = k1*a1 ≡
r2 - r1(mod a2)

由扩展欧几里得算法,,此方程有整数解,当且仅当
gcd(a1,a2) | r2 - r1,设gcd(a1,a2) = g

由模运算的性质,得
k1*a1/g ≡ (r2 - r1) / g (mod a2/g)

于是
k1 ≡ (r2 - r1) / g * (a1/g) ^ (-1) (mod a2/g)

用扩展欧几里得算法求出逆元,两两合并出解#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
using namespace std;

typedef long long LL;

int n;

void gcd(LL a,LL &x,LL b,LL &y,LL &g)
{
if (!b) {
x = 1;
y = 0;
g = a;
return;
}
gcd(b,x,a%b,y,g);
LL tmp = y;
y = x - a/b*y;
x = tmp;
}

LL getLL()
{
char ch = getchar();
if (ch == EOF) return -1;
LL ret = 0;
while (ch < '0' || '9' < ch) {
ch = getchar();
if (ch == EOF) return -1;
}
while ('0' <= ch && ch <= '9')
ret = ret*10LL + 1LL*(ch - '0'),ch = getchar();
return ret;
}

char ch[22];
void Print(LL x)
{
int len = 0;
for (; x; x /= 10LL)
ch[len++] = x % 10LL;
for (int i = len - 1; i >= 0; i--)
putchar(ch[i] + 48);
puts("");
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif

for (int I = 1; I; I++) {
n = getLL();
if (n == -1) break;
LL a1 = getLL();
LL r1 = getLL();
if (n == 1) {
if (r1 >= a1) puts("-1");
else Print(r1);
continue;
}
bool flag = 1;
for (int i = 2; i <= n; i++) {
LL a2 = getLL();
LL r2 = getLL();
if (!flag) continue;
LL x,y,g;
gcd(a1,x,a2,y,g);
LL R = r2 - r1;
if (R % g != 0) {flag = 0; continue;}
LL t = a2 / g;
x = ((R / g * x) % t + t) % t;
r1 = x * a1 + r1;
a1 = a1 / g * a2;
r1 = (r1 % a1 + a1) % a1;
}
if (!flag) puts("-1");
else Print(r1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: