当前位置:网站首页>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 代码
边栏推荐
- Memory problems difficult to locate, it is because you do not use ASAN
- OSSCore 开源解决方案介绍
- Double.doubleToLongBits()方法使用
- chart.js水平柱状图插件
- How can an organization judge the success of data governance?
- 突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
- Emulate stm32 directly with proteus - the programmer can be completely discarded
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- HDU 1520 Anniversary party (树型dp)
- Load balancing principle analysis and source code interpretation
猜你喜欢
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
mysql5.7安装部署-yum安装
Pycharm终端出现PS问题、conda或activate不是内部命令问题..
"MySQL Advanced Chapter" 6. Principles of index creation and design
[Azure Cloud] What is the difference between a service endpoint and a private link?point of view (1)
leetcode:334. 递增的三元子序列
Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
Taro小程序跨端开发入门实战
[Microservice Architecture] Microservices and SOA Architecture (2)
3 injured in 'electrical accident' at Google data center
随机推荐
C#List的使用以及Linq的使用
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
Memory problems difficult to locate, it is because you do not use ASAN
bus事件总线 使用
Techches Transformer the join wisdom source the author cao, visual basic model study
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
CodeChef STRMRG String Merging (dp)
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
Codeforces 814 C. An impassioned circulation of affection (dp)
FastReport.Net 2022.2.17 Crack
从产品维度来看 我们为什么不能完全信任Layer2?
what is rtems
TCP/IP笔记
what is bsp in rtems
MySQL executes the query process
「首席工程师」首席(Principal )工程师修炼之道
[Microservice Architecture] Microservices and SOA Architecture (2)
【勇敢饭饭,不怕刷题之链表】有序链表的合并
使用cpolar远程连接群晖NAS(升级固定链接2)
第2章-矩阵及其运算-矩阵运算(2)