poj2891非互质同余方程

时间:2022-05-22 23:32:06
Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8176   Accepted: 2439

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 ≤ ik) 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 ≤ ik).

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
解法:

举个例子,合并同余方程组

x%A=a   ①

x%B=b   ②

现在给出两种合并的方法:

1) 要把①②式合并成    x%C=c    ③         易知C一定是A和B的最小公倍数的倍数,否则不可能同时满足①②两式。

这里我们取C为A,B的最小公倍数,设d=gcd(A,B),则C=A*B/d.

接下来我们只要求出余数c即可,假设p是方程组①②的其中一个解

因为③是①②两式的合并方程,所以p也是③的解,所以可以得到c=p%C

接下来的问题就是怎么求出方程组①②的其中一个解。

首先,满足方程组①的最小解显然就是x=a

然后满足①②的解就是  (a+kA)%B=b,其中x=a+kA(k为任意自然数)

这个方程很明显可以用扩展欧几里德算法即可求得x,这样就完成了两个方程的合并

当所有的同余方程合并成一个方程 x%G=g  时候,g即为最终的最小解。。


 #include <iostream>
#include <math.h>
using namespace std;
#define ll long long int
ll funa(ll a,ll b)
{
if(b==) return a;
return funa(b,a%b);
}
void fun(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
return ;
}
fun(b,a%b,x,y);
ll t=x;
x=y;
y=t-(ll)(a/b)*y;
}
int main()
{
ll n;
while(cin>>n)
{
int i;
ll a[n][];
for(i=;i<n;i++)
cin>>a[i][]>>a[i][];
for(i=;i<n;i++)
{
ll z=funa(a[i-][],a[i][]);
if((a[i][]-a[i-][])%z!=)
break;
ll x,y;
fun(a[i-][],a[i][],x,y);
x=x*(a[i][]-a[i-][])/z;
x=(x%(a[i][]/z)+a[i][]/z)%(a[i][]/z);
x=x*a[i-][]+a[i-][];
a[i][]=a[i-][]*a[i][]/z;
a[i][]=x;
}
if(i>=n)
cout<<a[n-][]<<endl;
else cout<<-<<endl;
} } //(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n 。


														
		

poj2891非互质同余方程的更多相关文章

  1. POJ 2891 中国剩余定理的非互质形式

    中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...

  2. HDU5668 Circle 非互质中国剩余定理

    分析:考虑对给定的出圈序列进行一次模拟,对于出圈的人我们显然可以由位置,编号等关系得到一个同余方程 一圈做下来我们就得到了n个同余方程 对每个方程用扩展欧几里得求解,最后找到最小可行解就是答案. 当然 ...

  3. POJ 2891- Strange Way to Express Integers CRT 除数非互质

    题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...

  4. 数学--数论--HDU1825(积性函数性质&plus;和函数公式&plus;快速模幂&plus;非互质求逆元)

    As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...

  5. hdu 1573 X问题 &lpar;非互质的中国剩余定理&rpar;

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. poj 2891 Strange Way to Express Integers &lpar;非互质的中国剩余定理&rpar;

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

  7. LA 3720 高速公路(互质判斜率)

    https://vjudge.net/problem/UVALive-3720 题意: 有一个n行m列的点阵,问一共有多少条非水平非垂直的直线至少穿过其中的两个点. 思路: 没思路的题. 首先枚举矩形 ...

  8. as&plus;bt&equals;1是ab两数互质的充要条件

    [as+bt=1是ab两数互质的充要条件] 充分性,as+bt=1 => (a,b)=1: 因为as+bt=1,设c=(a,b),则c整除a和b,所以c整除as+bt,即c整除1,所以c=1,即 ...

  9. HDU3579Hello Kiki(中国剩余定理)(不互质的情况)

    One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...

随机推荐

  1. java中的反射简单实例

    package club.reflection.entity.User; /** * 实体类 * */ public class User { public String name; private ...

  2. jq 实现上下排序的一段代码

    前台页面: <div class="adddaren_box"> {%if isset($masterDetailsInfo)%} <div class=&quo ...

  3. Play1&plus;angularjs&plus;bootstrap &plus;&plus; &lpar;idea &plus; livereload&rpar;

    我的web开发最强组合:Play1+angularjs+bootstrap ++ (idea + livereload) 时间 2012-12-26 20:57:26  Freewind.me原文   ...

  4. 【温故而知新-Javascript】使用地理定位

    地理定位(Geolocation)API让我们可以获取用户当前地理位置的信息(或者至少是正在运行浏览器的系统的位置).它不是HTML5规范的一部分,但经常被归组到与HTML5相关的新功能中. 1. 使 ...

  5. UVa 1595 &lpar;水题&rpar; Symmetry

    颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...

  6. Deep Learning and Shallow Learning

    Deep Learning and Shallow Learning 由于 Deep Learning 现在如火如荼的势头,在各种领域逐渐占据 state-of-the-art 的地位,上个学期在一门 ...

  7. 修改TOMCAT服务器图标为应用LOGO

    在tomcat下部署应用程序,运行后,发现在地址栏中会显示tomcat的小猫咪图标.有时候,我们自己不想显示这个图标,想换成自己定义的的图标,那么按如下方法操作即可: 参考网上的解决方案:1.将$TO ...

  8. Nginx学习——Nginx启动、停止、重启和信号控制以及平滑升级

    1.Nginx 启动与停止 (1)启动方式 启动格式:Nginx可执行文件地址 -c Nginx配置文件地址 /etc/local/nginx/sbin/nginx -c /root/dufy/ngi ...

  9. 启动时候报错由于没有扫包 error creating bean with name

    <!-- 扫描包,加载service实现类 --> <context:component-scan base-package="com.taotao.search.serv ...

  10. vue组件里定时器销毁问题

    我在a页面写一个定时,让他每秒钟打印一个1,然后跳转到b页面,此时可以看到,定时器依然在执行.这样是非常消耗性能的.如下图所示: 解决方法1: 首先我在data函数里面进行定义定时器名称: data( ...