链接:

https://vjudge.net/problem/POJ-2891

题意:

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?

思路:

考虑同余方程组:

\(x \equiv a_1(mod m_1)\)

\(x \equiv a_2(mod m_2)\)

...

当求第i个式子时,我们有前i-i个方程的特解\(x\),通解\(x+i*m\),\(m\)为前i-1个方程\(m\)的lcm。

考虑第i个式子,\(x+t*m \equiv a_i (mod m_i)\),解除最小的t即可。

上式可转为\(t*m + (-k)*m_i = a_i-x\),用扩展欧几里得即可得到最小解。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h> using namespace std;
typedef long long LL;
const int INF = 1e9; const int MAXN = 1e5+10;
LL A[MAXN], M[MAXN];
int n; LL ExGcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
LL d = ExGcd(b, a%b, x, y);
LL tmp = x;
x = y;
y = tmp-(a/b)*y;
return d;
} LL ExCRT()
{
LL res = A[1], m = M[1];
for (int i = 2;i <= n;i++)
{
LL d, x, y;
d = ExGcd(m,M[i], x, y);
if ((A[i]-res)%d)
return -1;
x = x*(A[i]-res)/d;
//cout << x << ' ' << y << ' ' << d << endl;
x = (x%(M[i]/d)+(M[i]/d))%(M[i]/d);
res = res+x*m;
m = (m*M[i])/d;
res %= m;
}
return (res%m+m)%m;
} int main()
{
while(~scanf("%d", &n))
{
for (int i = 1;i <= n;i++)
scanf("%lld%lld", &M[i], &A[i]);
printf("%lld\n", ExCRT());
} return 0;
}

POJ-2891-Strange Way to Express Integers(线性同余方程组)的更多相关文章

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

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

  2. poj——2891 Strange Way to Express Integers

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

  3. [POJ 2891] Strange Way to Express Integers

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

  4. POJ 2891 Strange Way to Express Integers (解一元线性方程组)

    求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1)     1x=r2(mod a2)     ...

  5. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  6. POJ2891Strange Way to Express Integers (线性同余方程组)

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...

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

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  8. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  9. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  10. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

随机推荐

  1. SpringBoot中使用@Scheduled创建定时任务

    SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...

  2. Word 中批量修改所有表格格式样式

    1. 引言 我们在使用Word排版编写书籍时候,可能会带有许多表格,上百,甚至上千个表格都是有可能的.这么多的表格对于后期的样式修改是非常不利的,有什么好的方法能够一次性修改文档中所有的表格,将其统一 ...

  3. C++知识点总结篇

    const在不同位置时的不同意义 指针类型前:声明一个指向常量的指针,程序中不能通过指针来改变它所指向的值,但指针本身的值可以改变,即指针可以指向其他数据: "*"号和指针名之间, ...

  4. Python18之函数定义及调用,注释

    一.函数定义 def 函数名(形参1,形参2...): 函数体 return 返回值         (可以返回任何东西,一个值,一个变量,或是另一个函数的返回值,如果函数没有返回值,可以省略retu ...

  5. RHEL 6.5 安装Docker

    一,配置远程yum源二,下载依赖包1.安装downloadonly插件使用yum下载rpm包2.下载docker需要的依赖包三,安装docker(离线节点)1. 依次执行docker的安装包2. 启动 ...

  6. T-SQL学习笔记

    学习T-SQL时记录的笔记,记得并不全也不详细 if和while语句 declare @age int select @age = DATEDIFF(year,stuAge,getdate()) fr ...

  7. Bootsrap表格表单及其使用方法

    bootstrap的使用 bootstrap中的js插件依赖于jQuery 因此jQuery要在bootstrap之前引入 参考官网标准引入方法和引入样式 排版 标题 Bootstrap和普通的HTM ...

  8. 通过 Kubeadm 安装 K8S 与高可用,版本1.13.4

    环境介绍: CentOS: 7.6 Docker: 18.06.1-ce Kubernetes: 1.13.4 Kuberadm: 1.13.4 Kuberlet: 1.13.4 Kuberctl: ...

  9. vmware vSphere Data Protection 6.1--------2-初始化

    一.简介 安装完vdp接下来就是部署初始化了 安装篇请参考:vmware vSphere Data Protection 6.1部署 二.开始初始化 登陆https://192.168.216.200 ...

  10. Java内存模型学习笔记(一)—— 基础

    1.并发编程模型的分类 在并发编程中,我们需要处理两个关键的问题:1.线程间如何通信,2.线程间如何同步.通信是指线程之间以何种机制来交换信息,同步是指程序用于不同线程之间操作发生相对顺序的机制. 在 ...