题意:求解一般模线性同余方程组

解题关键:扩展中国剩余定理求解。两两求解。

$\left\{ {\begin{array}{*{20}{l}}
{x = {r_1}\,\bmod \,{m_1}}\\
{x = {r_2}\,\bmod \,{m_2}}
\end{array}} \right.$

为了代码的符号清晰,将转化后的系数都为正,故如下设方程。

$\left\{ {\begin{array}{*{20}{l}}
{x = {r_1} - {k_1}{m_1}}\\
{x = {r_2} + {k_2}{m_2}}
\end{array}} \right.$

${r_1} - {r_2} = {k_2}{m_2} + {k_1}{m_1}$

以上其实是另一个模线性同余方程组。

考虑$ax + by = c$

由模线性同余方程的存在定理:$\gcd (a,b)|c$

$\begin{array}{l}
\frac{a}{{\gcd (a,b)}}x + \frac{b}{{\gcd (a,b)}} = \frac{c}{{\gcd (a,b)}}\\
x \equiv {(\frac{a}{{\gcd (a,b)}})^{ - 1}}\frac{c}{{\gcd (a,b)}}\bmod (\frac{b}{{\gcd (a,b)}})
\end{array}$

回归原式:

$x$即为${k_1}$

推出原式中的$x$

$x = {r_1} - {k_1}{m_1} = {r_1} - {m_1}{(\frac{{{m_1}}}{{\gcd ({m_1},{m_2})}})^{ - 1}}\frac{{{r_2} - {r_1}}}{{\gcd ({m_1},{m_2})}}\bmod \frac{{{m_1}{m_2}}}{{\gcd ({m_1},{m_2})}}$

一般化这个结论,模数即为lcm,可直接记住

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
ll x,y,r[],m[],n;
ll extgcd(ll a,ll b,ll &x,ll &y){
ll d=a;
if(b) d=extgcd(b,a%b,y,x),y-=a/b*x;
else x=,y=;
return d;
}
ll excrt(int n,ll *m,ll *r){
ll M=m[],pre=r[],d;//a是模数
for(int i=;i<n;i++){
d=extgcd(M,m[i],x,y);
if((pre-r[i])%d!=) return -;
x=(pre-r[i])/d*x%m[i];
pre-=x*M;
M=M/d*m[i];//lcm
pre%=M;
}
return (pre%M+M)%M;
}
int main(){
ios::sync_with_stdio();
while(cin>>n){
for(int i=;i<n;i++) cin>>m[i]>>r[i];
ll ans=excrt(n,m,r);
printf("%lld\n",ans);
}
}

[poj2891]Strange Way to Express Integers(扩展中国剩余定理)的更多相关文章

  1. POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)

    题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...

  2. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  3. POJ2891 Strange Way to Express Integers (扩展欧几里德)

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia 题目大意 求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r ...

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

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

  5. POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  6. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

  7. P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers

    P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...

  8. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  9. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

随机推荐

  1. Android setTag()与getTag(),与set多个setTag()

    首先我们要知道setTag方法是干什么的,SDK解释为 Tags Unlike IDs, tags are not used to identify views. Tags are essential ...

  2. 2014年java软件project师面试题收集

    如果页面中于五个li标签.写个js使点击每个li返回他的index <!doctype html> <html> <head> <style> li{c ...

  3. C#使用for循环移除HTML标记

    public static string StripTagsCharArray(string source) { char[] array = new char[source.Length]; int ...

  4. JS基础知识再整理..........不断更新中

    1.JS的五种基本数据类型:字符串.数值.布尔.null.underfined. 2.在JS中,字符串.数值.布尔三种数据类型,有其属性和方法: 3.字符串的三种常用方法[.indexof()..su ...

  5. github commit, issue, pull request, project

    1 github的提供给用户操作和交流的几个对象 commit, issue, pull request and project 2 commit and commit comment commit就 ...

  6. ubuntu安装opencv(自己编译)

    如果只需要python的,只需pip install opencv-python,然后apt-get一下下面第一点的东东. 1/ sudo apt-get install build-essentia ...

  7. SpringBoot学习笔记(10):使用MongoDB来访问数据

    SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...

  8. vue 组件与传值

    一.表单输入绑定(v-model 指令) 可以用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定. ...

  9. 获取原生的DOM方式,DIY脚手架,vue-cli的使用

    一 . 获取原生的DOM的方式 在js中,我们可以通过id, class 或者标签获取DOM元素,vue中也为我们提供了获取原生DOM的方法,就是给标签或者组件添加 ref 属性,通过 this.$r ...

  10. Wannafly挑战赛12 B T95要减肥 【贪心】

    链接:https://www.nowcoder.com/acm/contest/79/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...