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. QQ对话代码

    <li> <strong class="QQ">客服QQ</strong> <span> <a href="mqqw ...

  2. Python之路:Python 基础(三)-文件操作

    操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 文件句柄 = file('文件路径', '模式') # 还有一种方法open 例1.创建文件  f = file('myfile. ...

  3. 报错:loaded the "" nib but didn't get a UITableView

    在加载OpenPosition界面的时候报错:loaded the "" nib but didn't get a UITableView 原因: If you have a NI ...

  4. 高级UNIX环境编程2

    perror("error:") ;  strerror 日历时间:time_t (1970.1.1开始的秒数)     struct timeval (秒数和微秒)  struc ...

  5. NET Core,跨平台的轻量级RPC

    NET Core,跨平台的轻量级RPC:Rabbit.Rpc 特性一览 Apache License 2.0协议开源 支持客户端负载均衡(提供了轮询.随机算法的实现) 支持ZooKeeper和文件共享 ...

  6. float 保留两位小数

    1.页面运算格式化数字 页面上有时候会用到数字的运算,运算过后会出现1.5999999999999这么长的数字,需要格式化数字,比如保留两位有效数字 首先导入这个标签 <%@ taglib ur ...

  7. [置顶] JAVA概述(6)常量,关键字,进制转换

    21.关键字.... 变量: 就是将不确定的数据进行存储.也就是需要在内粗恩中开辟一个空间. 整数类型   1个字节 byte                    占 8位 -128~~~127 2 ...

  8. android 时间滚动控件 底部弹出

    下载地址:http://download.csdn.net/detail/ldd119/7440895 转载请说明出处 先上个效果图 watermark/2/text/aHR0cDovL2Jsb2cu ...

  9. c++,static 静态成员变量 / 静态成员函数

    静态成员变量: //静态成员变量(static) // //1.如果想在同类的多个对象之间实现数据共享 ,可以用静态 //成员变量,即用static修饰的成员变量,例 static int a; // ...

  10. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...