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

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 a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (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

Hint

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

Source

 
今天最后一个中国剩余定理、、、、、、(还是水体。。。。。)

题目大意: 有一个数mod ri 等于ai  ,求这个数,若求不出来输出“-1”。

代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 1000
#define ll long long
using namespace std;
ll n,a[N],m[N];
ll read()
{
    ll x=,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    )
    {
        x=,y=;
        return a;
    }
    ll r=exgcd(b,a%b,x,y),tmp;
    tmp=x,x=y,y=tmp-a/b*y;
    return r;
}
ll crt()
{
    ll a1=a[],m1=m[],a2,m2,c,d;
    ;i<=n;i++)
    {
        m2=m[i],a2=a[i];
        c=a2-a1;ll x=,y=;
        d=exgcd(m1,m2,x,y);
        ;
        x=x*c/d;
        int mod=m2/d;
        x=(mod+x%mod)%mod;
        a1+=x*m1;m1*=mod;
    }
    ) a1+=m1;
    return a1;
}
int main()
{
    while(~scanf("%lld",&n))
    {
        ;i<=n;i++)
         m[i]=read(),a[i]=read();
        printf("%lld\n",crt());
    }
    ;
}

最新文章

  1. GitHub上那些值得一试的JAVA开源库--转
  2. android gradle的全局管理
  3. 如何基于RabbitMQ实现优先级队列
  4. Prerequisites?[HDU1144]
  5. 机器人学 —— 机器人视觉(Bundle Adjustment)
  6. openssl安装问题导致nginx添加ssl模块失败
  7. santoku学习笔记
  8. ASP.NET-FineUI开发实践-3
  9. android 获取屏幕尺寸
  10. shonc项目中的设计资讯模块 php 字符串操作与正则表达式 strip_tags preg_match
  11. c++一些面试题目
  12. JavaScript判断IE各版本完美解决方案
  13. groovy学习(四)io
  14. 谈谈HashMap与HashTable
  15. c++(查找)
  16. C#,一份超简单的数据库帮助类,SqlHelp
  17. [C++]数据结构-排序:插入排序之直接插入排序
  18. Hadoop生态圈-使用FreeIPA安装Kerberos和LDAP
  19. Codeforces Round #496 (Div. 3)
  20. boost range zhuan

热门文章

  1. BFS POJ 2251 Dungeon Master
  2. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
  3. C#基础 out传值
  4. WebApi实现IHttpControllerSelector问题
  5. hihocoder offer收割编程练习赛13 D 骑士游历
  6. (1)麻省理工:计算机科学和 Python 编程导论
  7. 字符集编码---3 Windows BOM
  8. Spartan6系列之Spartan6系列之芯片时钟资源深入详解
  9. 4星|《OKR工作法》:关注公司的真正目标,以周为单位做计划和考核
  10. c++中的类型转换--reinterpret_cast