[POJ 2891] Strange Way to Express Integers
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 10907 | Accepted: 3336 |
Description
Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
- Line 1: Contains the integer k.
- Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2
8 7
11 9
Sample Output
31
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define ll long long
#define N 10010 ll exgcd(ll a,ll b,ll& x, ll& y)
{
if(b==)
{
x=;
y=;
return a;
}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
bool solve(ll &m0,ll &a0,ll m,ll a)
{
ll y,x;
ll g=exgcd(m0,m,x,y);
if((a-a0)%g) return ;
x*=(a-a0)/g;
x%=m/g;
a0=(x*m0+a0);
m0*=m/g;
a0%=m0;
if(a0<) a0+=m0;
return ;
}
bool MLES(ll m[],ll r[],ll &m0 ,ll &a0,ll n)
{
m0=;
a0=;
bool flag=;
for(ll i=;i<n;i++)
{
if(!solve(m0,a0,m[i],r[i]))
{
flag=;
break;
}
}
return flag;
}
int main()
{
ll n;
ll m[N],r[N];
while(scanf("%lld",&n)!=EOF)
{
for(ll i=;i<n;i++)
{
scanf("%lld%lld",&m[i],&r[i]);
}
ll m0,a0;
ll flag=MLES(m,r,m0,a0,n);
if(flag) printf("%lld\n",a0);
else printf("-1\n");
}
return ;
}
[POJ 2891] Strange Way to Express Integers的更多相关文章
- 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(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- 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 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- 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 <cstdio> #include <cstring> #include <map> #include <cma ...
随机推荐
- wxPython 基本框架与运行原理 -- App 与 Frame
<wxPython in Action> chapter 1.2 笔记 wxPython 是 wxWidgets 的 Python 实现,“w” for Microsoft Windows ...
- 后台启动mysql ,redis
mysqld_safe --user=mysql & redis.conf daemonize no修改为daemonize yes
- 如何在windows下安装GIT
如何在windows下安装GIT 分步阅读 Git是一个免费的.开源的版本控制软件.在Windows上安装git,一般为msysgit,官方下载地址为 http://code.google.com/p ...
- willMoveToParentViewController 与 didMoveToParentViewController
在iOS 5.0以前,我们在一个UIViewController中这样组织相关的UIView 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候被盖 ...
- Visual Studio 中TODO List的使用
http://msdn.microsoft.com/en-us/library/txtwdysk.aspx 工欲善其事,必先利其器 When the Task List is open, you ca ...
- 1.2 XmlBeanFactory的实例化
源代码分析,是一件既痛苦又快乐的事情,看别人写的代码是通过的,但当你能够看明白的时候,相信快乐也会随之而来,为了减少痛苦,更快的带来快乐, 本文以spring框架的XmlBeanFactory为入手点 ...
- 贪心算法——将正整数变为1
题目链接http://toutiao.com/a6320936270101528833/ 为避免链接失效,再粘贴一下题目内容: 给你一个数n,有3种操作: 1.这个数加1 2.这个数减1 3.如果这个 ...
- FaceNet--Google的人脸识别(转)
引入 随着深度学习的出现,CV领域突破很多,甚至掀起了一股CV界的创业浪潮,当次风口浪尖之时,Google岂能缺席.特贡献出FaceNet再次刷新LFW上人脸验证的效果记录. 本文是阅读FaceNet ...
- unity3d与eclipse协同工作环境
原地址:http://bbs.9ria.com/thread-212576-1-1.html 这个过程非常复杂.步骤一定要谨记 1,建立一个unity3d工程,然后自己丢点模型进去吧.然后设置导出时候 ...
- php采集远程文章简单类
<?php /** * 采集类 * @author Milkcy * @copyright (C) 2012-2015 TCCMS.COM * @lastmodify 2012-07-10 14 ...