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. 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

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

题意非常easy,给出k组 a r  每组代表 x ≡ r (mod a) ;当中要注意的就是全部的a不一定互素,由于a不互素就不能直接用中国剩余定理来做,查了非常多资料,感觉数学家的思维不是凡人能够理解的,还是自己写一下计算的过程

首先来计算两组 x ≡ r1 ( mod a1 ) ; x ≡  r2 ( mod a2 ) ; 定义变量 k1 k2 得到 x = k1 * a1 + r1 ; x = k2 * a2 + r2 ; 由上面的等式得到 k1 * a1 + r1 = k2 * a2 + r2 ; 转化为 k1*a1 = (r2 - r1) + k2 *a2 ; 对左右取模a2,由于 (k2*a2)%s2 = 0 ,所以等式转化为  k1 * a1 ≡ ( r2 - r1 ) (mod a2) ;使用扩展欧几里得能够求解到
k1的值(推断是否存在k1的值),将k1带回到 x1 = k1 * a1 + r1 ;得到同一时候满足于{ x = k1 * a1 + r1 ; x = k2 * a2 + r2 ; }的一个特解 , 所以 x ≡ x1 (mod lcm(a1,a2) ) ; 也就是 x ≡ ( k1*a1+r1 ) ( mod ( a1*a2/d ) );这样也就将两个同余式转化为了一个,通过不断的转化,将k个等式合并为一个 ,用扩展欧几里得求出最小的正解x

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(b == 0)
{
d = a ;
x = 1 ;
y = 0 ;
}
else
{
gcd(b,a%b,d,y,x);
x = -x ;
y = -y ;
y += (a/b)*x ;
}
return ;
}
int main()
{
LL k , a1 , a2 , r1 , r2 , d , x , y ;
while(scanf("%I64d", &k)!=EOF)
{
LL flag = 1 ;
scanf("%I64d %I64d", &a1, &r1);
k-- ;
while(k--)
{
scanf("%I64d %I64d", &a2, &r2);
gcd(a1,a2,d,x,y);
if( (r2-r1)%d )
flag = 0 ;
if( flag )
{
x = (r2-r1)/d*x ;
y = a2/d ;
x = ( x%y +y)%y ;
r1 = x*a1 + r1 ;
a1 = (a1*a2)/d ;
}
}
gcd(1,a1,d,x,y);
if( r1%d )
flag = 0 ;
if(flag == 0)
printf("-1\n");
else
{
x = r1/d*x ;
y = a1 / d ;
x = ( x%y+y )%y ;
printf("%I64d\n", x);
}
}
return 0;
}

数论F - 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. 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】

    题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...

  3. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

  4. 一本通1635【例 5】Strange Way to Express Integers

    1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...

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

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  6. poj——2891 Strange Way to Express Integers

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

  7. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  8. [POJ 2891] Strange Way to Express Integers

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

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

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

随机推荐

  1. kinect for windows - SkeletonBasics-D2D详解之一

    之前的文章介绍了深度图的获取,但是深度图只是提供了一些数据,这些数据给上层应用使用还是偏底层一些,我们希望在这个基础上,获取一些信息,比如手挥动,人跑步,或者运动等等,那么这个文章开始我们来讲述kin ...

  2. Binary Tree Level Order Traversal II --leetcode C++

    考察点 广度优先遍历--层次遍历 STL内容器的用法 广度优先遍历的时候,首先应该想到的就是借助于队列.还需要在遍历下一层之前保存当前层节点的数量 代码很简单: class Solution { pu ...

  3. tomcat使用所遇到的问题

    1.在使用eclipse添加server的时候添加不了tomcat服务器: 如图左图所示,添加按钮已成灰色,无法操作. 解决办法:找到workspace的工作空间->.metadata-> ...

  4. CodeForce 569A

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Litt ...

  5. 猪猪的机器学习笔记(十七)隐马尔科夫模型HMM

    隐马尔科夫模型HMM 作者:樱花猪 摘要: 本文为七月算法(julyedu.com)12月机器学习第十七次课在线笔记.隐马尔可夫模型(Hidden Markov Model,HMM)是统计模型,它用来 ...

  6. jquery自定义分页插件

    //每次只显示5个页码(function ($) { //设定页码方法,初始化 $.fn.setPager = function (options) { var opts = $.extend({}, ...

  7. thinkphp中Conf的配置

    -----www ----------admin -------------Conf ----------admin.php ----------Home -------------Conf ---- ...

  8. LNMP下基于端口的虚拟主机配置

    1.在/usr/local/nginx/conf/nginx.conf文件的的最后一个"}"前加上 include vhost/*.conf; 2.在/usr/local/ngin ...

  9. python 爬一下

    1.安装Requestswindow:pip install requestslinux:sudo pip install requests国内安装缓慢,建议到:http://www.lfd.uci. ...

  10. BCB6.0是垃圾的二十条理由

    我用的BCB6.0和windows2000 都是公司配的正版,我也有多年的开发经验. 1. IDE常常出现非法操作,有时重起动还会出错,须要重装BCB. 2. 自己主动完毕和智能提示功能超慢,慢到能够 ...