http://poj.org/problem?id=2891 (题目链接)

题意

  求解线性同余方程组,不保证模数一定两两互质。

Solotion

  一般模线性方程组的求解,详情请见:中国剩余定理

细节

  注意当最后发现方程无解直接退出时,会导致有数据没有读完,然后就会Re,所以先用数组将所有数据存下来。

代码

// poj2891
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
LL a[maxn],r[maxn],n; void exgcd(LL a,LL b,LL &d,LL &x,LL &y) {
if (b==0) {d=a;x=1;y=0;return;};
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
LL CRT() {
LL M=a[1],R=r[1];
LL d,x,y;
for (int i=2;i<=n;i++) {
LL mm=a[i],rr=r[i];
exgcd(M,mm,d,x,y); //M*x+R=mm*y+rr
if ((rr-R)%d!=0) return -1;
x=((rr-R)/d*x%(mm/d)+mm/d)%(mm/d); //求出最小正整数x
R+=M*x; //把整个M*x+R当做余数
M*=mm/d; //lcm(M,m)
}
return R;
}
int main() {
while (scanf("%lld",&n)!=EOF) {
for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i],&r[i]);
printf("%lld\n",CRT());
}
return 0;
}

  

  

【poj2891】 Strange Way to Express Integers的更多相关文章

  1. 【POJ2891】Strange Way to Express Integers(拓展CRT)

    [POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...

  2. 【poj2891】Strange Way to Express Integers

    题意: 给出n个模方程x=a(mod r) 求x的最小解 题解: 这就是个线性模方程组的模版题- - 但是有一些要注意的地方 extgcd算出来的解x可能负数  要让x=(x%mo+mo)%mo 而且 ...

  3. 【POJ】【2891】Strange Way to Express Integers

    中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} ...

  4. 一本通1635【例 5】Strange Way to Express Integers

    1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...

  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. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  7. 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】

    题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...

  8. 1635:【例 5】Strange Way to Express Integers

    #include<bits/stdc++.h> #define ll long long using namespace std; ll n,m,a,lcm,now; bool flag; ...

  9. 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)

    题意:Elina看一本刘汝佳的书(O_O*),里面介绍了一种奇怪的方法表示一个非负整数 m .也就是有 k 对 ( ai , ri ) 可以这样表示--m%ai=ri.问 m 的最小值. 解法:拓展欧 ...

随机推荐

  1. Fidder--实现手机的抓包

    今天闲着没吊事,来写一篇关于怎么抓取Android中的app数据包?工欲行其事,必先利其器,上网google了一下,发现了一款神器:Fiddler,这个貌似是所有软件开发者必备神器呀!这款工具不仅可以 ...

  2. POJ 1125 Stockbroker Grapevine

    Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33141   Accepted: ...

  3. Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  4. 集锦.txt

    不同的时代,有不同的需要以前,我们都以为自己可以为爱情去死,可是后来爱情死了,我们还活着. 小时候男孩子喜欢电动玩具,女孩子喜欢娃娃.长大了男孩子喜欢娃娃,女孩子喜欢电动玩具.也许这就是成长吧! 我大 ...

  5. How to configure SRTM elevations in WorldWind WMS

    In this thread I will try to explain how to serve SRTM elevations using NASA WorldWind WMS. ! Import ...

  6. Caffe学习系列(21):caffe图形化操作工具digits的安装与运行

    经过前面一系列的学习,我们基本上学会了如何在linux下运行caffe程序,也学会了如何用python接口进行数据及参数的可视化. 如果还没有学会的,请自行细细阅读: caffe学习系列:http:/ ...

  7. <转载> 你应该更新的Java知识之常用程序库(一)

    原文出处:http://www.blogbus.com/dreamhead-logs/226738702.html 很多人眼中,Java已经是一门垂垂老矣的语言,但并不妨碍Java世界依然在前进.如果 ...

  8. Windows下虚拟机安装Mac OS X ----- VM12安装Mac OS X 10.11

    Windows下虚拟机安装Mac OS X -– VM12安装Mac OS X 10.11 随着Iphone在国内大行其道,越来越多的开发者涌入iOS开发大军 中,但都苦于没有苹果机,本文即将介绍WI ...

  9. Chrome扩展开发之一——Chrome扩展的文件结构

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  10. 阿里云OSS NET SDK 引用示范程序

    1.引入SDK bin文件,下载地址:https://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/internal/oss/0.0.4/assets/sdk/ ...