[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 ...
随机推荐
- Catalyst揭秘 Day4 analyzer解析
Catalyst揭秘 Day4 analyzer解析 今天继续解析catalyst,主要讲一下analyzer,在sql语句的处理流程中,analyzer是在sqlparse的基础上,把unresol ...
- css 面试学习
最经在学习前端的一些东西 转载于http://www.cnblogs.com/lei2007/archive/2013/08/16/3262897.html 雅虎的css前端的35条定律
- 数据库 mysql 优化器原理
MySQL查询优化器有几个目标,但是其中最主要的目标是尽可能地使用索引,并且使用最严格的索引来消除尽可能多的数据行. 你的最终目标是提交SELECT语句查找数据行,而不是排除数据行.优化器试图排除数据 ...
- centos startx 进入桌面后使用中文输入法
(从runlevel 3进入桌面:英文 ,结果用不了中文输入法 囧) 但在中文桌面下可以输入中文 步骤: 由startx可以进入桌面 查看:which startx => /usr/bin/st ...
- c++ string char* const char*
#include <iostream> #include <string> #include <cstring> using namespace std; int ...
- [JavaScript] js 判断闰年
/** * 判断闰年函数 * @param {number} year 要判断的年份 * @return {bool} 返回布尔值 * * 其实只要满足下面几个条件即可. * 1.普通年能被4整除且不 ...
- 破解之寻找OEP[手动脱壳](1)
OEP:(Original Entry Point),程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) 代表程序的 ...
- ASP + ACCESS保存图片文件之实现
con.execute "CREATE tblImg (lngId COUNTER PRIMARY KEY, binImg IMAGE)" set ads=createobject ...
- SGU 180
求逆序数对 归并排序 #include <cstdio> #include <cstring> #include <cmath> #include <a ...
- POJ1228+凸包
见代码. /* 凸包(稳定凸包) 题意:给出一些点,这些点要么是凸包的顶点要么是边上的. 证明每条边上都至少有3个点. */ #include<stdio.h> #include<s ...