数论F - Strange Way to Express Integers(不互素的的中国剩余定理)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d
& %I64u
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.
题意非常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(不互素的的中国剩余定理)的更多相关文章
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- 「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 ...
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- 一本通1635【例 5】Strange Way to Express Integers
1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
随机推荐
- 在cnblog中使用syntax方法
<pre name="code" class="brush: cpp;"> 代码 </pre> #include<cstdio&g ...
- Microsoft Deployment Toolkit 2013 Preview Release Now Available
MDT 2013 provides a common console with comprehensive tools and guidance for every organizational ro ...
- Why is celsius = 5 * (fahr - 32) / 9 ?
Go to my personal blog There is a program to print Fahrenheit-Celsius table as below. #include <s ...
- Android Blur效果之FastBlur
Blur 自从iOS系统引入了Blur效果,也就是所谓的毛玻璃.模糊化效果,磨砂效果,各大系统就開始竞相模仿,这是一个怎样的效果呢,我们现来看一些图: 这些就是典型的Blur效果,在iOS和MIUI中 ...
- php随机10-thinkphp 3.1.3 模板继承 布局
8.25 模板继承 模 板继承是3.1.2版本添加的一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类 的继承一样,模板也可以定义一个 ...
- r语言之条件、循环语句
if条件语句:if (conditon) {expr1} else {expr2} > x<-1> if(x==1)+ {x<-"x=1"}else+ {x ...
- js触屏事件
js的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend.这三个事件最重要的属性是 pageX和 pageY,表示X,Y坐标. touchstart在触摸开始 ...
- QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)
QT弹出模态对话框做法: 1.新建UI文件时,一定要选择基类是QDialog的,我的选择是:Dialog without Buttons(),如下图: 2.然后在使用的时候: MyDialog dlg ...
- Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序
原文 Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序 原文地址: http://channel9.msdn.com/Series/Windows- ...
- JAE京东云引擎Git上传管理代码教程和京东云数据库导入导出管理
文章目录 Git管理准备工作 Git工具上传代码 发布代码装程序 mywebsql管理 京东云引擎小结 JAE京东云引擎是京东推出的支持Java.Ruby.Python.PHP.Node.js多语 ...