poj 2891 Strange Way to Express Integers(中国剩余定理)
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(中国剩余定理)的更多相关文章
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- POJ 2891 Strange Way to Express Integers(中国剩余定理)
题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
随机推荐
- JavaSE学习总结第16天_集合框架2
16.01 ArrayList存储字符串并遍历 ArrayList类概述:底层数据结构是数组,查询快,增删慢,线程不安全,效率高 ArrayList类是List 接口的大小可变数组的实现.实现了所 ...
- java执行windows 的cmd 命令
//获取运行时 Runtime rt = Runtime.getRuntime(); //获取进程 Process p = rt.exec(String[] cmdarray); 或者 P ...
- python实现单向链表
#Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x self ...
- 替换NavigationController里面的返回按钮
通过navigationController push进来的controller,默认的返回按钮是将本controller pop出去. 但有时候想在pop出去前完成一些自己的一些事情,这时可以自己写 ...
- CentOS 6.5(64bit)安装GCC4.8.2+Qt5.2.1(替换GCC的链接库)
截至目前,Qt的最新版本为5.2.1,CentOS的版本为6.5,GCC的版本为4.8.2,经过一番尝试,终于将Qt开发环境安装到了CentOS(64 bit)中,整个过程中有几个需要注意的地方,在这 ...
- 利用Adapter对象将数据填充到DataTable(或DataSet)的例子
前: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataAdapter ...
- 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档
对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...
- UISearchBar去除底部黑线问题
有时我们在设置搜索框的时候底部会出现一条黑线,要 去除这黑线只需设置. [self.searchBar setBackgroundImage:[UIImage new]];
- BZOJ 3040: 最短路(road) ( 最短路 )
本来想学一下配对堆的...结果学着学着就偏了... 之前 kpm 写过这道题 , 前面的边不理它都能 AC .. 我也懒得去写前面的加边了... 用 C++ pb_ds 库里的 pairing_hea ...
- PHP学习笔记6-时间/日期
时区/时间/日期 输出unix时间戳(从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数),用time() echo time();//unix时间戳 输出结果:143557475 ...