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.

题目大意:给k个线性同余方程,求这些方程的公共解。

附思路:http://blog.csdn.net/orpinex/article/details/6972654

思路:考虑两个方程的情况

ans = r1(mod a1)
ans = r2(mod a2)①
存在k1使得ans = r1 + k1 * a1
把ans代入①得:r1 + k1 * a1 = r2(mod a2)
k1 * a1 = r2 - r1(mod a2)
存在k2使得k1 * a1 - k2 * a2 = r2 - r1
利用拓展欧几里得求出k1(为了得到最小的非负整数k1,可以让k1 = k1 mod (a2/gcd(a1, a2)))

那么令ans = r1 + k1 * a1

对于多个方程的情况,两个两个地联立解,new_r = ans = r1 + k1 * a1, new_a = lcm(a1, a2)

解析:

关于为了让k1最小要k1 = k1 mod (a2/gcd(a1, a2))。令d = gcd(a1, a2),a1'= a1 / d,a2' = a2 / d ,r' = (r2 - r1) / d,对方程k1 * a1 - k2 * a2 = r2 - r1,两边同时除以d得k1 * a1' - k2 * a2' = r',即k1 * a1' = r' (mod a2'),对于任意解k1 = x',有通解x = x' + a2' = x' + a2 / gcd(a1, a2)。则最小的k1 = (x' + a2 / gcd(a1, a2)) mod (a2 / gcd(a1, a2) = x' mod (a2 / gcd(a1, a2))

关于new_a = lcm(a1, a2)。考虑方程x * a = y * b,两边同时除以gcd(a, b),得到x * a' = y * b',x / y = b' / a',那么有x = kb',y = ka',k∈Z。那么使得方程x * a = y * b成立的x * a = k * b' * a = k * lcm(a, b)。容易想象,p + ax = q + by的每个合理的p + ax的差为lcm(a, b)。即对于方程r1(mod a1) = r2(mod a2)的解也是隔lcm(a1, a2)就出现一个解,即new_a = lcm(a1, a2)。

代码(16MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if(!b) d = a, x = , y = ;
else {
exgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} int main() {
LL k, a1, a2, r1, r2;
while(scanf("%I64d", &k) != EOF) {
bool flag = true;
scanf("%I64d%I64d", &a1, &r1);
for(int i = ; i < k; ++i) {
scanf("%I64d%I64d", &a2, &r2);
if(!flag) continue;
LL r = r2 - r1, d, k1, k2;
exgcd(a1, a2, d, k1, k2);
if(r % d) flag = false;
LL t = a2 / d;
k1 = (r / d * k1 % t + t) % t;
r1 = r1 + a1 * k1;
a1 = a1 / d * a2;
}
printf("%I64d\n", flag ? r1 : -);
}
}

POJ 2891 Strange Way to Express Integers(拓展欧几里得)的更多相关文章

  1. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  2. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  3. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  4. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  5. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  6. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  7. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  8. POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】

    求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...

  9. POJ 2891 Strange Way to Express Integers(中国剩余定理)

    题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...

随机推荐

  1. asp.net mvc4 HTTP Error 403.14

    asp.net mvc4项目部署到II&上时,出现HTTP Error 403.14 - Forbidden - The Web server is configured to not lis ...

  2. Linux 获取设备树源文件(DTS)里描述的资源

    Linux 获取设备树源文件(DTS)里的资源 韩大卫@吉林师范大学 在linux使用platform_driver_register() 注册 platform_driver 时, 需要在 plat ...

  3. RSA加密算法原理及RES签名算法简介

    第一部分:RSA算法原理与加密解密 一.RSA加密过程简述 A和B进行加密通信时,B首先要生成一对密钥.一个是公钥,给A,B自己持有私钥.A使用B的公钥加密要加密发送的内容,然后B在通过自己的私钥解密 ...

  4. 通过hibernate session.connection()获得数据库连接时,导致的查询缓慢甚至假死机问题

    在使用hibernate的应用中,如果需要直接使用Java.sql.Connection,一般我们是通过hibernate的session.connection()获得的,然后session.clos ...

  5. NodeJS优缺点及适用场景讨论

    概述:NodeJS宣称其目标是“旨在提供一种简单的构建可伸缩网络程序的方法”,那么它的出现是为了解决什么问题呢,它有什么优缺点以及它适用于什么场景呢? 本文就个人使用经验对这些问题进行探讨. 一. N ...

  6. 一键搞定JavaEE应用,JRE+Tomcat+Mysql-JaveEE绿色运行环境JTM0.9版

    本文转载自[http://www.blogjava.net/huliqing/archive/2009/01/09/247375.html] 这是一个集成了jre,tomcat,mysql的绿色运行环 ...

  7. Python中的socket 模块

    Python 提供了两个基本的 socket 模块.第一个是 Socket,它提供了标准的 BSD Sockets API.第二个是 SocketServer, 它提供了服务器中心类,可以简化网络服务 ...

  8. R中list对象属性以及具有list性质的对象

    R语言list的特点:It has length, [[ and [ methods, and is recursive because list can contain other list!上图显 ...

  9. WebApp 设计要素

    从去年开始就负责公司WebApp的产品跟设计工作,最近整体大改了两个版本,也算累积了一些实际的经验.在不断学习的过程中,发现对于WebApp可以直接用于项目上的资料比较零碎,在这里总结一下,供初做 W ...

  10. Uc爆破工具

    #coding:utf-8 import requests import sys from threading import Thread from Queue import Queue NUM=5 ...