Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 19509   Accepted: 6592

Description

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ ik) 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 ≤ ik).

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

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

模不互素不能用CRT,所有就有了我解线性方程组的方法?

但是有个关键步骤不太懂。。只是会用这个板子。。

 #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include<iostream>
#include <cmath>
#include<string>
#define ll long long
#define dscan(a) scanf("%d",&a)
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
#define MAXL 1105
#define Endl endl
#define maxn 1000005
ll x,y;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>'') {if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<='') {x=*x+ch-'';ch=getchar();}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==) {
x=;y=;return a;
}
ll d=exgcd(b,a%b,x,y);
ll temp=x;
x=y;
y=temp-a/b*y;
return d;
}
int main()
{
ll k,a,b,c,d;
while(~scanf("%lld",&k))
{
cin>>a>>b;
//cout<<"hhh"<<Endl;
int flag=;
for(ll i=;i<k;++i)
{
c=read();d=read();
if(flag) continue;
ll r=d-b;
ll hhh=exgcd(a,c,x,y);
if(r%hhh) {flag=;continue;}
ll tmp=c/hhh;
x=((r/hhh*x)%tmp+tmp)%tmp;//不理解
b=a*x+b;
a=a*c/hhh;
}
if(flag) cout<<"-1"<<endl;
else cout<<b<<endl;
}
}

POJ 2891的更多相关文章

  1. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  2. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  3. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  4. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  5. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  6. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  7. POJ 2891 中国剩余定理(不互素)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 17877 ...

  8. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  9. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  10. 一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)

    2891 -- Strange Way to Express Integers import java.math.BigInteger; import java.util.Scanner; publi ...

随机推荐

  1. 新装Ubuntu后的一些配置

    一:Ubuntu 16.04 开启root用户和使用root用户登陆 1. 编辑/etc/lightdm/lightdm.conf autologin-guest=false autologin-us ...

  2. 【C++学习笔记】强大的算法——spfa

    spfa的定义 PFA算法的全称是:Shortest Path Faster Algorithm,用于求单源最短路,由西南交通大学段凡丁于1994年发表.当给定的图存在负边时,Dijkstra算法就无 ...

  3. 新装NGINX重启,出现错误 nginx: [error] open() "/usr/local/nginx/logs/nginx.pid"

    重装nginx出现,重启出现错误 ./nginx -s reload nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" ...

  4. ASP.NET 自定义路由 RouteBase

    适用场景:当前项目有一个接口:http://xxx.com/a.aspx,现在我们在不需要a.aspx这个文件的,直接处理这个地址的请求,我们可以做很多的事情,比如,直接返回一个静态的JSON文件内容 ...

  5. 删除Zend Studio项目

    导入了过大的项目,导致很卡,且Close Project和Delete操作不了,一直无响应. 调整项目目录下的隐藏文件夹,删除对应项目: E:\www\.metadata\.plugins\org.e ...

  6. 本地Navicat连接虚拟机MySQL

    安装完MySQL后,使用mysql命令进去,然后执行以下命令 grant all privileges on hive_metadata.* to 'hive'@'%' identified by ' ...

  7. day09-函数讲解

    1.如何定义一个函数 s = '华为加油a' def s_len(): i = 0 for k in s: i += 1 print(i) s_len() 这个函数的功能就是输出字符串的长度.但是他只 ...

  8. [原]sencha touch之布局

    今天记录一下关于sencha touch中的几种布局,其实很简单的,还是直接上代码,一目了然 1:box布局,其实就是vbox和hbox,说白了一个是横着摆放,一个是竖着摆放 Ext.applicat ...

  9. Maya材质

    mental ray--Indirect Lighting(物理学太阳天空)      Final Gathering最终聚集   改变质量为production的,FG就是关闭需要重新打开 平行光, ...

  10. Hyper-V:利用差异磁盘安装多个Win2008

    签于成本的原因,在学习了解一项新的技术或是产品时,在没有部署到生产环境之中前,大家都会选择在虚拟机来搭建一套实验环境.但如何快速搭建呢?如何节省磁盘空间呢? 说到此都不得不说下Hyper-V的差异磁盘 ...