当前位置:网站首页>POJ 2891 Strange Way to Express Integers (Extended Euclidean)
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
2022-08-10 11:09:00 【51CTO】
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
Sample Output
题意
给出一组 mi,ri ,Find the smallest positive integer X ,使得 X%mi=ri ,如果不存在这样的 X 则输出 −1
思路
Because the divisors are not mutually primed,Therefore, the template of the Chinese remainder theorem cannot be directly applied!
Here we use merge 不定方程 的方法,That is, combine the first two equations first,The combined result is then combined with the third equation,Calculated after the final merge X
我们假设 mi 是除数, ri
X%m0=r0 与 X%m1=r1 ,等价于 X=m0×k0+ro=m1×k1+r1 ,其中 ki
联立可得 m0×k0+m1×k1=r1−r0 (因为 ki
Extended Euclidean Algorithm:Integer pairs exist (x,y) 使得 ax+by=gcd(a,b)
So we can construct it according to the above formulas m0x+m1y=gcd(m0,m1)
通过 ex_gcd 可以求出 gcd(m0,m1) 以及 x,y
于是便有 k0=x×r1−r0gcd(m0,m1)
Bringing back the original formula can be solved X=m0×k0+r0 ,此时 X 的通解为 X′=X+k×lcm(m0,m1) ( k 为一整数,想要让 X′%m0=r0,X′%m1=r1 These two formulas are established at the same time,X The step size for each increment should be lcm(m0,m1)
The above formula can be transformed into : X′%lcm(m0,m1)=X
So the two equations are merged~
After merging all the equations,输出最小的正整数 X′
Ask about the code k0 modmigcd(M,mi)
a×x0+b×y0=gcd(a,b) 等同于 a×x0+a×bgcd(a,b)k+b×y0−a×bgcd(a,b)k=gcd(a,b)
即 a×(x0+bgcd(a,b)k)+b×(y0−agcd(a,b)k)=gcd(a,b)
通解为 x=x0+bgcd(a,b)k , y=y0−agcd(a,b)k ,其中 k=...−2,−1,0,1,2...
So in all solutions x 的最小正整数解为 (x0+bgcd(a,b))%bgcd(a,b) , y
所以 modmigcd(M,mi) 保证了 k0
AC 代码
边栏推荐
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- owl.carousel海报卡片Slider轮播切换
- Text selection rounded style border-radius
- [C language] Floating point number rounding
- 3 injured in 'electrical accident' at Google data center
- 内存问题难定位,那是因为你没用ASAN
- Several small projects that I have open sourced over the years
- owl.carousel poster card Slider carousel switch
- 动作捕捉系统用于室内组合定位技术研究
- Redis(三)——配置文件详解、发布和订阅、新数据类型
猜你喜欢
Memory problems difficult to locate, it is because you do not use ASAN
In August the DB list latest scores - database Engines
owl.carousel海报卡片Slider轮播切换
Unsafe的一些使用技巧
Taro小程序跨端开发入门实战
这些年我开源的几个小项目
技能大赛训练题:组策略一
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
[Azure Cloud] What is the difference between a service endpoint and a private link?point of view (1)
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
随机推荐
chart.js水平柱状图插件
这些年我开源的几个小项目
C#实战:基于ItextSharp技术标签生成小工具
dedecms supports one-click upload of Word content
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
The impact of development mode on testing
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
database transaction
STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
HCIP ---- VLAN
Regarding the missing json converter, the error message is: No converter found for return value of type
Gartner reiterates the important value of 'data weaving'
The usage and difference between getParameter() and getAttribute()
mysql5.7安装部署-yum安装
"MySQL Advanced Chapter" 6. Principles of index creation and design
getParameter()与 getAttribute()的用法与区别
PTA 7-2 Summation and Counting of Diagonal Elements of Square Matrices Solution
"Scalability" extensibility best practices: lessons from eBay
Redis6(一)——NoSQL数据库简介与Redis的安装
Memory problems difficult to locate, it is because you do not use ASAN