【POJ】2891 Strange Way to Express Integers
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的更多相关文章
- POJ——T 2891 Strange Way to Express Integers
http://poj.org/problem?id=2891 Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16849 ...
- 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(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- 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' ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
随机推荐
- Linux内核驱动之GPIO子系统(一)GPIO的使用
转自:http://blog.csdn.net/mirkerson/article/details/8464290 一 概述 Linux内核中gpio是最简单,最常用的资源(和 interrupt , ...
- SublimeText 插件
Markdown 预览 想要预览,可以配置下面的快捷方式. { "keys": ["alt+m"], "command": "ma ...
- java中 this 和super的用法
通过用static来定义方法或成员,为我们编程提供了某种便利,从某种程度上可以说它类似于C语言中的全局函数和全局变量.但是,并不是说有了这种便利,你便可以随处使用,如果那样的话,你便需要认真考虑一下自 ...
- Apache与Tomcat联系及区别(转)
Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行.Apache是Web服务器而Tomcat是Java应用服务器. A ...
- NuGet学习笔记(1) 初识NuGet及快速安装使用
关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...
- Codeforces Round #370 (Div. 2) D. Memory and Scores DP
D. Memory and Scores Memory and his friend Lexa are competing to get higher score in one popular c ...
- HDU 5875 Function st + 二分
Function Problem Description The shorter, the simpler. With this problem, you should be convinced ...
- 静态局部变量、静态全局变量、extern全局变量、自动变量 札记
静态局部变量 静态局部变量. 从称呼上我们可以看出,静态局部变量首先是一个局部变量,因此其只在定义它的函数内有效,冠以静态的头衔后,其生存期就被延长了,不会随着函数的返回而被撤销.我们可以这样来理解: ...
- Ubuntu中如何打开终端terminal
法一 先按住Alt,然后再按F2,出来一个运行框,在里面输入gnome-terminal即可 [编辑]法二 如果想从右键菜单中打开终端,需要安装一个软件: sudoapt-get install na ...
- poj2533 LIS
题目链接: http://poj.org/problem?id=2533 题意:第一个数n,接下来n个数,> ....求最长上升子序列. 这道题有两种解法,第一种是通解,也适用于别的LIS. ...