题意

Language:Default
Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 21651 Accepted: 7266

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.

Source

分析

excrt裸题,直接套模板即可。

时间复杂度\(O(n \log n)\)

代码

#include<iostream>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;
    rg char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
        data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x){
    return x=read<T>();
}
typedef long long ll;
ll exgcd(ll a,ll b,ll&x,ll&y){
    if(!b){
        x=1,y=0;
        return a;
    }
    ll d=exgcd(b,a%b,x,y);
    ll z=x;x=y;y=z-a/b*y;
    return d;
}
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    int n;
    while(~scanf("%d",&n)){
        ll a,b,x,y,k;
        read(a),read(b);
        ll lcm=a,ans=b;
        bool flag=1;
        --n;
        while(n--){
            read(a),read(b);
            b=(b-ans%a+a)%a;
            ll d=exgcd(lcm,a,x,y);
            if(b%d) flag=0;
            else k=x*(b/d)%a;
            ans+=k*lcm;
            lcm=lcm/d*a;
            ans=(ans%lcm+lcm)%lcm;
        }
        printf("%lld\n",flag?ans:-1);
    }
    return 0;
}

POJ2891 Strange Way to Express Integers的更多相关文章

  1. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

  2. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  3. P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers

    P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...

  4. POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  5. POJ2891 - Strange Way to Express Integers(模线性方程组)

    题目大意 求最小整数x,满足x≡a[i](mod m[i])(没有保证所有m[i]两两互质) 题解 中国剩余定理显然不行....只能用方程组两两合并的方法求出最终的解,刘汝佳黑书P230有讲~~具体证 ...

  6. POJ2891 Strange Way to Express Integers [中国剩余定理]

    不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...

  7. POJ2891 Strange Way to Express Integers【扩展中国剩余定理】

    题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...

  8. [poj2891]Strange Way to Express Integers(扩展中国剩余定理)

    题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...

  9. poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组

    怎样求同余方程组?如: \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \cdots \\ x \equ ...

随机推荐

  1. zw版【转发·台湾nvp系列Delphi例程】HALCON SubImage

    zw版[转发·台湾nvp系列Delphi例程]HALCON SubImage procedure TForm1.Button1Click(Sender: TObject);var op : HOper ...

  2. 2017-2018-1 JaWorld 团队作业--冲刺1

    2017-2018-1 JaWorld 团队作业--冲刺1(20162301) 一.小组分工: 由于小组成员在本周比较忙碌,所以考虑后本组的冲刺就按照每天加入不同的类来完成.我们根据类的难度大小来决定 ...

  3. sqlite中的时间

    插入时间的sql语句 ','-61') 时间格式'2014-11-17T19:37:32' 年月日和时分秒之间多了一个字母T,保存到数据库的时候,会自动给时间加8个小时. 保存的结果为2014-11- ...

  4. 【查看内存】Linux查看内存使用情况(一)

    用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...

  5. POJ 3436 ACM Computer Factory(最大流+路径输出)

    http://poj.org/problem?id=3436 题意: 每台计算机包含P个部件,当所有这些部件都准备齐全后,计算机就组装完成了.计算机的生产过程通过N台不同的机器来完成,每台机器用它的性 ...

  6. Linux的硬链接和软链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link), 也就是软链接.默认情况下,ln命令产生硬链接. [硬连接]硬连 ...

  7. 这些HTML、CSS知识点,面试和平时开发都需要 No10-No11(知识点:表格操作、代码编写规则)

    系列知识点汇总 1.基础篇 这些HTML.CSS知识点,面试和平时开发都需要 No1-No4(知识点:HTML.CSS.盒子模型.内容布局) 这些HTML.CSS知识点,面试和平时开发都需要 No5- ...

  8. Python——列表表达式

    https://www.cnblogs.com/xuyuanyuan123/p/6718403.html

  9. Java研发工程师知识点总结

    Java研发工程师知识点总结 最近一次更新2017年12月08日 大纲 一.Java基础(语言.集合框架.OOP.设计模式等) 二.Java高级(JavaEE.框架.服务器.工具等) 三.多线程和并发 ...

  10. UVA-1617 Laptop (贪心)

    题目大意:有n条长度为1的线段,n个区间,第i条线段在第i个区间中,问线段之间的最少间隙有几个. 题目分析:先对区间排序,先按右端点排,再按左端点排.有重叠的区间(仅有交点重叠也视为重叠)之间一定可以 ...