http://poj.org/problem?id=2891

题意:求解一个数x使得 x%8 = 7,x%11 = 9;
   若x存在,输出最小整数解。否则输出-1;

ps:

思路:这不是简单的中国剩余定理问题,由于输入的ai不一定两两互质,而中国剩余定理的条件是除数两两互质。
   这是一般的模线性方程组,对于
    X mod m1=r1
    X mod m2=r2
    ...
    ...
    ...
    X mod mn=rn
首先,我们看两个式子的情况
X mod m1=r1……………………………………………………………(1)
X mod m2=r2……………………………………………………………(2)
则有 
X=m1*k1+r1………………………………………………………………(*)
X=m2*k2+r2
那么 m1*k1+r1=m2*k2+r2
整理,得
m1*k1-m2*k2=r2-r1
令(a,b,x,y,m)=(m1,m2,k1,k2,r2-r1)。原式变成
ax+by=m
熟悉吧? 此时,由于GCD(a,b)=1不一定成立,GCD(a,b) | m 也就不一定成立。所以应该先判 若 GCD(a,b) | m 不成立,则!! 。方程无解! !! 。
否则,继续往下。 解出(x,y),将k1=x反代回(*)。得到X。
于是X就是这两个方程的一个特解,通解就是 X'=X+k*LCM(m1,m2)
这个式子再一变形,得 X' mod LCM(m1,m2)=X
这个方程一出来。说明我们实现了(1)(2)两个方程的合并。 令 M=LCM(m1,m2)。R=r2-r1
就可将合并后的方程记为 X mod M = R。 然后,扩展到n个方程。
用合并后的方程再来和其它的方程按这种方式进行合并,最后就能仅仅剩下一个方程 X mod M=R,当中 M=LCM(m1,m2,...,mn)。
那么,X便是原模线性方程组的一个特解,通解为 X'=X+k*M。 假设,要得到X的最小正整数解,就还是原来那个方法: X%=M;
if (X<0) X+=M;


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#define LL long long
#define _LL __int64
#define eps 1e-8 using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10; _LL k;
_LL M; _LL extend_gcd(_LL a,_LL b,_LL &x,_LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
_LL r = extend_gcd(b,a%b,x,y);
_LL t = x;
x = y;
y = t-a/b*y;
return r;
}
} int main()
{
_LL a1,m1,a2,m2,x,y,i,d;
while(scanf("%lld",&k)!= EOF)
{
bool flag = true;
scanf("%lld %lld",&m1,&a1);
for(i = 1; i < k; i++)
{
scanf("%lld %lld",&m2,&a2); d = extend_gcd(m1,m2,x,y); if((a2-a1)%d != 0)
flag = false; _LL t = m2/d;
x *= (a2-a1)/d;
x = (x%t + t)%t;
//注意新的m1,a1是怎么得来的
a1 = x*m1+a1;
m1 = m1*m2/d;
a1 = (a1%m1+m1)%m1;
}
if(flag == true)
printf("%lld\n",a1);
else printf("-1\n"); }
return 0;
}

poj 2891 Strange Way to Express Integers(中国剩余定理)的更多相关文章

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

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

  2. POJ 2891 Strange Way to Express Integers 中国剩余定理解法

    一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...

  3. POJ 2891 Strange Way to Express Integers(中国剩余定理)

    题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...

  4. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

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

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

  6. poj——2891 Strange Way to Express Integers

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

  7. [POJ 2891] Strange Way to Express Integers

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

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

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

  9. poj 2891 Strange Way to Express Integers【扩展中国剩余定理】

    扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...

随机推荐

  1. Python学习之路——字符处理(二)

    一.set集合: set是一个无序且不重复的元素集合 建立一个集合: x = set([1, 'tom', 2, 3, 4]) print(type(x)) print(x) 以上实例运行后反回结果结 ...

  2. Django : Table 'MyDjango.django_admin_log' doesn't exist

    原因: 添加admin之后,没有运行 manage.py syncdb 解决方法: 在命令行中运行manage.py syncdb 即可 运行截图:

  3. DataTable Select查询

    如果是Excel中数据,空数据为DBNull.Value 查询:dt.Select("name is null") 查询长度:dt.Select("len(convert ...

  4. 快捷查看dll的PublicKeyToken

    @echo off d: cd D:\Win2003\Microsoft Visual Studio 10.0\VC\ call vcvarsall.bat x86 echo. if not '%1' ...

  5. WebHdfs

    https://github.com/ihrwein/webhdfs https://tiborbenke.blogs.balabit.com/2013/11/the-syslog-ng-in-the ...

  6. hibernate Criteria查询 2.3

    Criteria对象提供了一种面向对象的方式查询数据库.Criteria对象需要使用Session对象来获得一个Criteria对象表示对一个持久化类的查询 查询所有 Session session ...

  7. i++和++i以及左值,右值

    左值(LValue)和右值(RValue)的一个快捷记法是赋值运算,左值是赋值运算左边的值,右值就是右边(=,=废话).例如: int a = 5; a就是左值,5就是右值. 当然,如果真是这么个含义 ...

  8. 【转】 随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...

  9. mysql 添加[取消]timestamp的自动更新

    创建自动更新的 timestamp (插入或修改时 uptime都会自动更新) CREATE TABLE `hello` (`id` int(11) NOT NULL,`uptime` timesta ...

  10. RGB,CMYK,HSB各种颜色表示的转换 C#语言

    Introduction Why an article on "colors"? It's the same question I asked myself before writ ...