[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 ...
随机推荐
- ubuntu 安装dell无线网卡2
以下转自:http://blog.sina.com.cn/s/blog_73b6331101016haq.html ubuntu 12.04 bcm43xx无线网卡安装记录 (2012-07-01 0 ...
- flex直接访问服务器
1.读取指定文件里的xml: <?xml version="1.0" encoding="utf-8"?><s:WindowedApplica ...
- Wpf 简单制作自己的窗体样式(2)
上一篇blog讲了制作简单的样式的窗体,对于一个传统的窗体,不仅仅可以拖动,和关闭操作.还具有最大化.最小化.隐藏,以及改变窗体的大小等.这篇blog就是对上篇的补充,完善窗体的改变大小和最大化最小化 ...
- matlab之点运算基本思想及几何平移变换
1.对数变换可以增强图像中较暗部分的细节,因为对数可以将较小的值放大,而较大的值缩小 2.伽马变换:y = (x + esp) ^ γ,x,y的取值范围是0到1,esp是补偿系数,γ为伽马系数.γ的不 ...
- Clone中存在的浅克隆问题
A.java package second; public class A { String country;//国家 String province;//地区 String city;//城市 pu ...
- kruskal算法-Pascal
马上就快要考试了,然而突然发现自己图论已经废了,于是再都打一遍练练手...... const maxn=; maxe=maxn*maxn; type edge=record //edge记录每一条边, ...
- 判断webpart类型 How can I tell what type a web part is?
using(new SPSite("http://mysite/myweb").OpenWeb()){ //give relative path of the webpartpag ...
- js模拟触发事件
html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件] 可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...
- Linux学习笔记(2)-用户和用户组
用户(user)和用户组(group)概念 linux是一个多用户操作系统,他允许多个用户登录linux系统进行各自不同的操作.为了方便管理用户不同的权限,组的概念应用而生,一个组可以包含多个用户,共 ...
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...