中国剩余定理的非互质形式

任意n个表达式一对对处理,故只需处理两个表达式。

x = a(mod m)

x = b(mod n)

km+a = b (mod n)

km = (a-b)(mod n)

利用扩展欧几里得算法求出k

k = k0(mod n/(n,m)) = k0 + h*n/(n,m)

x = km+a = k0*m+a+h*n*m/(n,m) = k0*m+a (mod n*m/(n,m))

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define LL long long using namespace std;
LL m0, m1, a0, a1; LL Exgcd(LL a,LL b,LL &x,LL &y) // ax+by = (a,b)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL x1,y1,x0,y0;
x0=1; y0=0;
x1=0; y1=1;
x=0; y=1;
LL r=a%b;
LL q=(a-r)/b;
while(r)
{
x=x0-q*x1; y=y0-q*y1;
x0=x1; y0=y1;
x1=x; y1=y;
a=b; b=r; r=a%b;
q=(a-r)/b;
}
return b;
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
scanf("%lld%lld", &m0, &a0);
bool flag = 0;
while(--n)
{
scanf("%lld%lld", &m1, &a1);
LL x, y, tmp;
LL d = Exgcd(m0, m1, x, y);
x = (x%(m1/d)+m1/d)%(m1/d);
tmp = ((a1-a0)%m1+m1)%m1;
if(tmp%d != 0)
flag = 1;
x = x*(tmp/d)%(m1/d);
a0 = (x*m0+a0+m0/d*m1)%(m0/d*m1);
m0 = m0/d*m1;
}
if(flag)
printf("-1\n");
else
printf("%lld\n", a0);
}
return 0;
}

POJ 2891 中国剩余定理的非互质形式的更多相关文章

  1. 中国剩余定理模数不互质的情况(poj 2891

    中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...

  2. POJ 2891 中国剩余定理(不互素)

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

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

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

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

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

  5. poj 1006中国剩余定理模板

    中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...

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

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

  7. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

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

  8. hdu 1573 X问题 (非互质的中国剩余定理)

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

  9. poj2891非互质同余方程

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

随机推荐

  1. php int 与 datetime 转换

    数据库日期类型是int类型的,该查询结果是datetime类型的 select from_unixtime( `dateline` ) from cdb_posts 如果原来类型是datetime类型 ...

  2. Mybatis 一对一、一对多、多对多

    一对一返回resultType <!-- 查询订单关联查询用户信息 resultType --> <select id="findOrderCustom" res ...

  3. BitmapFactory.decodeStream(inputStream)返回null的解决方法

    场景:Android,通过inputStream从网络上获取图片 随后两次使用BitmapFactory对InputStream进行操作,一次获取宽高,另一次缩放 但是在缩放时,发现inputStre ...

  4. Jmeter数据库mysql测试说明

    主要分3个步骤,详细操作步骤如下: 一.环境准备 1.下载mysql驱动,下载地址:https://dev.mysql.com/downloads/connector/j/,Select Operat ...

  5. [one day one question] Iscroll 5.0 在chrome上无法滑动

    问题描述: Iscroll 5.0 在chrome上无法滑动,不仅仅在chromePC的开发的时候,在手机上的chrome也有同样的问题,这怎么破? 解决方案: // 关闭 PointerEvent ...

  6. Windows下Yii2框架的两种安装方式及升级最新版

    第一种:归档文件形式安装(适合于没有安装composer的机器) 进入下载页https://www.yiiframework.com/download,选择下载第一个 下载完成之后是一个压缩包,解压文 ...

  7. 比较字符串CompareTo的用法及注意

    CompareTo用法 static void Main(string[] args)         {             string str = "1";        ...

  8. 树梅派配置ad-hoc网络

    树梅派配置ad-hoc网络 更新与安装 1.更改源/etc/apt/source.list: http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian ...

  9. Hibernate抽取BaseDao

    package com.cky.dao; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate. ...

  10. 转:MySQL 的show processlist

    processlist 命令的输出结果显示了有哪些线程在运行,可以帮助识别出有问题的查询语句,两种方式使用这个命令. 1.        进入 mysql/bin 目录下输入 mysqladmin p ...