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());
    }
    ;
}

poj——2891 Strange Way to Express Integers的更多相关文章

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

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

  2. [POJ 2891] Strange Way to Express Integers

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

  3. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

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

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

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

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

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

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

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

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

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

  8. 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' ...

  9. POJ 2891 Strange Way to Express Integers(中国剩余定理)

    题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...

随机推荐

  1. magento 翻译使用实例

    在自定义的模块中若想要使用翻译,需在config.xml中加入如下配置 <config> <adminhtml> //后台 <translate> <modu ...

  2. VMWare虚拟网络编辑

    VMWare虚拟网络编辑记录. 点击"编辑->虚拟网络编辑器" 在网卡列表中选择"VMnet8"查看目前设置,点击右下角的更改设置进行网络修改. 备注:更 ...

  3. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  4. P1400 塔

    题目描述 有N(2<=N<=600000)块砖,要搭一个N层的塔,要求:如果砖A在砖B上面,那么A不能比B的长度+D要长.问有几种方法,输出 答案 mod 1000000009的值. 输入 ...

  5. 转载pcb设计详细版

    http://www.51hei.com/bbs/dpj-52438-1.html 详细的altium designer制作PCB步骤,按照步骤一步步的学习就会自己制作PCB模型 目 录 实验三  层 ...

  6. 【PostgreSQL-9.6.3】物化视图

    PostgreSQL 9.3 以后出现了物化视图.物化视图是由实实在在的数据组成,这是和一般视图的根本区别. 1. 物化视图创建语法如下: --创建语法 CREATE MATERIALIZED VIE ...

  7. HDU_1584_(DFS)

    蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. MFC_2.6 使用菜单列表和控件

    使用菜单列表和控件 1.添加List Control控件 2.属性设置VIEW 为REPORT 3.初始化 // 1. 设置列表的扩展风格 m_ListCtrl.SetExtendedStyle(LV ...

  9. blender_(uv应用)................http://digitalman.blog.163.com/blog/static/23874605620174172058299/

    轻松学习Blender基础入门之九:UV-1 2017-06-21 14:24:49|  分类: Blender |举报 |字号 订阅     下载LOFTER 我的照片书  |   [前言]     ...

  10. 03Oracle Database 物理结构,逻辑结构

    Oracle Database 物理结构,逻辑结构 Oracle Database 物理结构 数据文件 Oracle数据库有一个或多个物理的数据文件.数据库的数据文件包含全部数据库数据.逻辑数据物理地 ...