当前位置:网站首页>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 代码
边栏推荐
- Redis (six) - transaction and lock mechanism of Redis6 (unfinished, to be supplemented)
- Break through the dimensional barriers and let the dolls around you move on the screen!
- Gartner再次重申了“数据编织”的重要价值
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- Three-phase 380V rectified voltage
- bus event bus use
- Store limited time seckill function system
- 【电商运营】你真的了解社交媒体营销(SMM)吗?
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- 1-IMU参数解析以及选择
猜你喜欢

Redis6(一)——NoSQL数据库简介与Redis的安装

首次入选OSDI顶会!腾讯提出超大规模推荐系统的模型低延时更新方案

动作捕捉系统用于室内组合定位技术研究

"Chief Engineer" Principal (Principal) engineer's way of training

组合模式:Swift 实现

HCIP ---- VLAN

3D rotating text animation js special effects

From the product dimension, why can't we fully trust Layer2?

owl.carousel poster card Slider carousel switch

4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
随机推荐
14 high-frequency handwritten JS interview questions and answers to consolidate your JS foundation
blocking non-blocking poll mechanism asynchronous
8月份DB-Engines 数据库排行榜最新战况
谷歌数据中心发生“电力事故”造成 3 人受伤
关于json转换器缺失的问题,报错内容:No converter found for return value of type
3 injured in 'electrical accident' at Google data center
POJ 3101 Astronomy (数学)
Will SQL and NoSQL eventually converge?
Redis (six) - transaction and lock mechanism of Redis6 (unfinished, to be supplemented)
第2章-矩阵及其运算-矩阵运算(2)
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
chart.js水平柱状图插件
HDU 1520 Anniversary party (树型dp)
Dry goods!ASSANet: Making PointNet++ faster and stronger
Text selection rounded style border-radius
what is bsp in rtems
三相380V整流后的电压
Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
Automated Testing and Selenium