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

题意:求最小的$x$使得$x \equiv r_i \pmod{ a_i }$。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
void ex(ll a, ll b, ll &d, ll &x, ll &y) {
if(!b) { d=a; x=1; y=0; return; }
ex(b, a%b, d, y, x); y-=a/b*x;
}
ll m[1000005], a[1000005];
int n;
int main() {
while(~scanf("%d", &n)) {
for(int i=0; i<n; ++i) scanf("%lld%lld", &m[i], &a[i]);
ll mm=m[0], aa=a[0]%m[0], d, x, y; int flag=1;
for(int i=1; i<n; ++i) {
ll r=a[i]-aa;
ex(mm, m[i], d, x, y);
if(r%d) { puts("-1"); flag=0; break; }
aa+=((x*(r/d)%m[i]+m[i])%m[i])*mm;
mm=mm/d*m[i];
aa%=mm;
}
if(flag) printf("%lld\n", aa);
}
return 0;
}

  

由$k_1a_1 + r_1 = x = k_2a_2 + r_2$构造出一个$x_0$,所以方程的解满足$x = x_0+k*lcm(a_1, a_2)$。向后递推即可。

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

  1. POJ——T 2891 Strange Way to Express Integers

    http://poj.org/problem?id=2891 Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16849   ...

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

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

  3. poj——2891 Strange Way to Express Integers

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

  4. [POJ 2891] Strange Way to Express Integers

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

  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【扩展欧几里德】【模线性方程组】

    求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...

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

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

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

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

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

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

随机推荐

  1. JqueryDemoTools-用于整理jQueryDemo 分类: C# 公共资源 2014-12-02 16:50 224人阅读 评论(1) 收藏

    应用背景: 在学习js时,为了熟悉某个功能,或使用某个插件,往往需要写一个Demo来测试:一些好的Demo也可以整理积累下来,方便以后查阅: 写了一个编写jQuery Demo的辅助工具.界面很简单, ...

  2. 背景虚化 Google Camera App Nokia Refocus HTC One M8 的 Duo景深相机

    背景虚化是单反中一种比较常见的拍照形式,参看 http://www.techbang.com/posts/%2017842 https://refocus.nokia.com/

  3. 在Salesforce中对Object实现Trigger的绑定

    Trigger的相关属性详细解读请看如下链接: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_c ...

  4. Silverlight中的TabControl如何绑定数据?重写tabcontrol和tabItem 解决绑定友好问题。可以绑定对象集合

    在 WPF 中,TabControl 可以直接将 ItemsSource 绑定数据源,见 将 TabControl 绑定到数据的示例 http://msdn.microsoft.com/zh-cn/l ...

  5. ZOOKEEPER3.3.3源码分析(四)对LEADER选举过程分析的纠正

    很抱歉,之前分析的zookeeper leader选举算法有误,特此更正说明. 那里面最大的错误在于,leader选举其实不是在大多数节点通过就能选举上的,这一点与传统的paxos算法不同,因为如果这 ...

  6. MPAndroidChart饼图属性及相关设置

    公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...

  7. LoadRunner编程之跳出迭代

    LoadRunner编程之跳出迭代 51Testing软件测试网3p6pK.Yo LoadRunner中 提供了函数exit(-1)来结束迭代. 使用return 0 来结束本次迭代,进入下一次迭代. ...

  8. css3写箭头

    左箭头 .left-arrow { position: absolute; left: 6%; top: 31%; overflow: hidden; zoom:; width: 0.4rem; he ...

  9. 【noip2014T3】

    上文有提到noip2014还有没A的嘛..就先把这个坑给填了 flappy bird好sad啊 还是先做解方程 八中的数据好强了,然而我最后凑了四个质数就A了,感谢shy! 作为联赛最后一题,学习它的 ...

  10. 使用dSYM分析App崩溃日志

    前言 我们在开发App过程中,因为连接到控制台,所以遇到问题会很容易找到问题代码.但是对于线上的App出现Crash的时候,我们不可能通过这种方式,也不现实,所以我们只能通过收集Crash信息,来解决 ...