[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 ...
随机推荐
- Linux进程间通信IPC学习笔记之同步一(线程、互斥锁和条件变量)
基础知识: 测试代码: 参考资料: Posix 多线程程序设计
- SPFA&邻接表 PASCAL
题目来自CODE[VS]-->热浪 1557 热浪 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 题目描述 Description 德克萨斯纯朴的民眾们这个 ...
- vijos 1085 Sunnypig闯三角关
{这个题5个正确,五个超时,不要盲目相信我的代码,谁有更好的算法或者优化请留言,(*^__^*) 嘻嘻……} 背景 贪玩的sunnypig请Charles为他打造一个奇幻世界,Charles欣然答应了 ...
- windows 2008 R2 Activition
无需破解:Windows Server 2008 R2 至少免费使用 900天 1.首先安装后,有一个180天的试用期. 2.在180天试用期即将结束时,使用下面的评估序列号激活Svr 2008 R2 ...
- jsf2入门视频 教程
jsf2.0 入门视频 教程 需要的看下.初次录视频.还有很多需要完善. JSF交流QQ群84376982 JSF入门视频下载地址 http://pan.baidu.com/s/1jG3y4T4 ...
- java当中的定时器的4种使用方式
import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask; p ...
- [转载]VS2012创建MVC3项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
如果在没有安装vs2012 update3升级包的情况下,创建MVC3项目会出现下面的错误信息. 因为VS2012已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用模块了,使用VS201 ...
- 团体程序设计天梯赛-练习集L1-007. 念数字
L1-007. 念数字 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 输入一个整数,输出每个数字对应的拼音.当整数为负数时,先 ...
- PHP 5.3连接sql server 2008 R2
我的机器为: xp sp3 sql server 2008 developer apache 2.2.2 php 5.3 从5.3开始,php就不再提供mssql.dll了,所以要php连接sql ...
- 用css制作三角形
用css制作三角形,主要是利用css元素给“盒模型”设置边框得到的. 上图,上边框和做边框,以及上边框和右边框的交合处,浏览器会按照直角的二分之一处绘制交合线.这是“盒模型”有宽和高时候的效果.我们假 ...